设置30秒计时器的代码



请使用JavaScript和HTML将所有页面与渐进的30秒计数计数计数链接起来,而无需在页面导航上重新计算。谢谢!

在评论中牢记更多信息,这是您的代码:

index.html中的某个地方

<div id="counter"></div>

yourscript.js

// Simplifies getting the value of a cookie
function getCookie(name)
  {
    var re = new RegExp(name + "=([^;]+)");
    var value = re.exec(document.cookie);
    return (value != null) ? unescape(value[1]) : null;
  }
// Max time for cookie
var count = 30;
if(document.cookie && document.cookie.match('counter')) {
    // Set value of counter to match the cookie
    count = getCookie('counter');
}
// Runs once per second
setInterval(function() {
  // Decrease the value of count by 1 every second  
  count--;
  // if count is less or equal to 0 we move to menu.html
  if(count <= 0) {
    window.location.href="menu.html"
  }
  // Set the text inside the <div id="counter"></div> to the value of counter
  document.getElementById('counter').innerHTML = count;
  // Set the value of the cookie to match the value of our counter variable
  document.cookie = 'counter=' + count;
}, 1000);

我为此创建了一个JSFIDDLE,但是由于某种原因,JSfiddle现在不想为我创建一个唯一的URL ...转到JSFiddle.net,然后将其复制到脚本字段中,而DIV则添加到HTML中区域,然后按几次运行,您会看到计数器不断倒数,在页面刷新之间持续存在。

最新更新