JQuery计时器倒计时器

时间:2016-10-27 12:30:08

标签: javascript jquery html

我想要一个简单的JQuery倒计时器,在刷新浏览器时不应该重启。我需要剩余天数,剩余小时数,剩余时间,剩余时间。

请帮我做 我做了一些代码,但它没有重置,我只做了几秒钟,请帮我做几分钟和几小时。 Jsfillde列在下面

https://jsfiddle.net/3jf2g7q1/4/

3 个答案:

答案 0 :(得分:0)

尝试吹小提琴,

https://jsfiddle.net/7fn039eu/

代码:

<p>
<span id="hour"></span> : <span id="mins"></span> : <span id="secs"></span>
</p>
<script>

function clock(){
var date=new Date();
console.log(date);
console.count("called");
var hours=date.getHours();
var min=date.getMinutes();
var sec=date.getSeconds();

document.getElementById('hour').innerText=hours;
document.getElementById('mins').innerText="0"+min;
document.getElementById('secs').innerText=sec;


}
clock();
setInterval(clock,1000);
</script>

答案 1 :(得分:0)

请看这个:
http://www.w3schools.com/jsref/met_win_setinterval.asp

并将其与: http://www.w3schools.com/js/js_dates.asp

如果必须,还有一个日期选择小工具!
https://jqueryui.com/datepicker/

答案 2 :(得分:0)

我已修改答案:this post将时间存储在本地存储中。这意味着如果您刷新浏览器,倒计时将在停止的地方继续。

<强> HTML

<body>
  <div><span id="time"></span></div>
</body>

<强>的JavaScript var time = 5

function startTimer(duration, display) {
  var timer = duration;
  setInterval(function() {
    minutes = parseInt(timer / 60, 10)
    seconds = parseInt(timer % 60, 10);

    minutes = minutes < 10 ? "0" + minutes : minutes;
    seconds = seconds < 10 ? "0" + seconds : seconds;

    display.textContent = minutes + ":" + seconds;

    localStorage.minutes = minutes;
    localStorage.seconds = seconds;
    //      localStorage.removeItem('minutes')
      //localStorage.removeItem('seconds')
    if (--timer < 0) {
        localStorage.removeItem('minutes')
      localStorage.removeItem('seconds')
      timer = 5;
    }

  }, 1000);
}

window.onload = function() {
  if (Number(localStorage.seconds) < time) {
    var seconds = Number(localStorage.minutes) * 60
    console.log(seconds)
    time = Number(localStorage.seconds) + seconds
  }
  display = document.querySelector('#time');
  startTimer(time, display);
};