【问题标题】:Set Pause option to Javascript Countdown Timer script将暂停选项设置为 Javascript 倒数计时器脚本
【发布时间】:2018-05-01 07:50:59
【问题描述】:

我正在创建一个倒数计时器。所以这是我目前的代码。

function countdownTimeStart() {

var el = document.getElementById('demo');
var cancel = document.getElementById('cancel');

/* Start count the time in timer panel */
var time = document.getElementById("picker-dates").value;
time = time.split(':');

var x = setInterval(function () {

    // set hours, minutes and seconds, decrease seconds
    var hours = time[0];
    var minutes = time[1];
    var seconds = time[2]--;

    console.log(time);

    // create a function to handle the cancel
    function cancelCountdown(){
        el.innerHTML = "00:00:00";
        clearInterval(x);
    }
    // attach listener to cancel if cancel button is clicked
    cancel.addEventListener( 'click', cancelCountdown);

    // if seconds are negative, set them to 59 and reduce minutes
    if (time[2] == -1) {
        time[1]--;
        time[2] = 59
    }

    // if minutes are negative, set them to 59 and reduce hours
    if (time[1] == -1) {
        time[0]--;
        time[1] = 59
    }

    // Output the result in an element with id="demo"
    if( seconds == 0 && minutes == 0 && hours == 0 ){
        clearInterval(x);
        el.innerHTML = "00:00:00";
    } else if (seconds < 10) {
        el.innerHTML = hours + ": " + minutes + ": " + "0" + seconds + " ";
    } else {
        el.innerHTML = hours + ": " + minutes + ": " + seconds + " ";
    }

}, 1000);}

所以我想为此创建一个暂停按钮。我提到了类似的问题,例如Javascript - Pausing setInterval()

在 jquery 中创建暂停选项似乎很容易。但我不知道如何将它应用到我的脚本中。谁能帮帮我。

【问题讨论】:

  • 保存间隔开始的时间,点击按钮取消间隔,计算应该有的时间间隔,开始新的间隔直到计算的时间结束
  • 能否请您指出方法作为答案。谢谢。
  • jQuery 在您链接到的答案中的唯一意义是将事件处理程序添加到几个按钮,这些按钮将 pause 标志设置为真或假——这个想法没有什么特别之处暂停计时器。只要你知道如何在没有 jQuery 的情况下添加事件处理程序,答案就在那里。

标签: javascript countdown pause


【解决方案1】:

在暂停点击时,您可以创建一个像savedTime 这样的对象来保存time 数组的当前值。然后,当点击start 时,您可以检查savedTime 中是否有任何内容。如果是这样,则将hoursminutesseconds 分配给该对象的值;否则,将它们的值设置为输入中的默认值。例如,类似:

var el = document.getElementById('demo');
var cancel = document.getElementById('cancel');
var savedTime;
function countdownTimeStart() {
  /* Start count the time in timer panel */
  var timeValue = document.getElementById("picker-dates").value;
  var time;
  if (savedTime) {
    time = savedTime;
    savedTime = null;
  } else time = timeValue.split(':');

  var x = setInterval(function () {
    // set hours, minutes and seconds, decrease seconds
    var hours = time[0];
    var minutes = time[1];
    var seconds = time[2]--;

    // rest of countdownTimeStart is as normal
    // ...
  });

  // Attach this function to your pause button:
  function pauseTimer() {
    savedTime = time;
    clearInterval(x);
  }
}

【讨论】:

  • 不适合我,我不应该添加“ var pause= document.getElementById('pause'); 吗?
  • 正如评论所说,你需要Attach this function to your pause button: - 你没有发布你的HTML,所以我把它留给了你。
  • 是我的暂停按钮,是否可以只添加“ var pause= document.getElementById('pause') ;"另外来自您的代码,
  • 您还需要将点击处理程序附加到按钮 - 只是声明元素不会做任何事情。
  • 它工作正常。再次点击暂停按钮可以恢复吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-08-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多