如何推送/播放setTimeout功能

时间:2014-06-23 11:00:30

标签: javascript php jquery

我想要显示一些不同的时钟。下面给出的代码对我来说很好。但是我想在用户想要的时候推动和玩这个时间。我想推送和恢复SetTimeout功能。任何人都知道如何做到这一点?

enter image description here

$("#push").click(function () {

});

$("#play").click(function () {
    show();
});

function show() {
    var Digital = new Date();
    var time2 = Digital.getTime();
    var time1 = 1403517957984;
    var diff = Math.abs(new Date(time2) - new Date(time1));
    var seconds = Math.floor(diff / 1000); //ignore any left over units smaller than a second
    var minutes = Math.floor(seconds / 60);
    seconds = seconds % 60;
    var hours = Math.floor(minutes / 60);
    minutes = minutes % 60;
    if (hours < 10) hours = "0" + hours;
    if (minutes < 10) minutes = "0" + minutes;
    if (seconds < 10) seconds = "0" + seconds;
    $('#worked_time').html(hours + ":" + minutes + ":" + seconds);
    setTimeout("show()", 1000);
}
show();

3 个答案:

答案 0 :(得分:2)

将句柄保存到变量:

var toHandle = null;

// start timeout
toHandle = setTimeout("show()", 1000);

然后您可以随时取消超时:

// cancel timeout
if (toHandle) {
  clearTimeout(toHandle);
  toHandle = null;
}

答案 1 :(得分:2)

这是@colburton和@TwiStar

的回应

正确回答答案

var isPlaying = true;
var toHandle = null;

$("#push").click(function () {
    isPlaying = false;
    if (toHandle !== null)
    {
       clearTimeout(toHandle);
       toHandle = null;
    }
});

$("#play").click(function () {
    isPlaying = true;
    show();
});

function show() {
    if (isPlaying) {
        toHandle = null;
        var Digital = new Date();
        var time2 = Digital.getTime();
        var time1 = 1403517957984;
        var diff = Math.abs(new Date(time2) - new Date(time1));
        var seconds = Math.floor(diff / 1000); //ignore any left over units smaller than a second
        var minutes = Math.floor(seconds / 60);
        seconds = seconds % 60;
        var hours = Math.floor(minutes / 60);
        minutes = minutes % 60;
        if (hours < 10) hours = "0" + hours;
        if (minutes < 10) minutes = "0" + minutes;
        if (seconds < 10) seconds = "0" + seconds;
        $('#worked_time').html(hours + ":" + minutes + ":" + seconds);

        toHandle = setTimeout("show()", 1000);
    }
}
show();

答案 2 :(得分:0)

快速而肮脏。你可以使用if来检查“我应该显示时间”-flag:

var doShow = true;
$("#push").click(function () {
    doShow = false;
});

$("#play").click(function () {
    doShow = true;
    show();
});

function show() {
  if(doShow == true) {


    var Digital = new Date();
    var time2 = Digital.getTime();
    var time1 = 1403517957984;
    var diff = Math.abs(new Date(time2) - new Date(time1));
    var seconds = Math.floor(diff / 1000); //ignore any left over units smaller than a second
    var minutes = Math.floor(seconds / 60);
    seconds = seconds % 60;
    var hours = Math.floor(minutes / 60);
    minutes = minutes % 60;
    if (hours < 10) hours = "0" + hours;
    if (minutes < 10) minutes = "0" + minutes;
    if (seconds < 10) seconds = "0" + seconds;
    $('#worked_time').html(hours + ":" + minutes + ":" + seconds);
    setTimeout("show()", 1000);

   }
}
show();