javascript – 如何推送/播放setTimeout函数

前端之家收集整理的这篇文章主要介绍了javascript – 如何推送/播放setTimeout函数前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

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

   

$("#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();
最佳答案
这是@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();
原文链接:https://www.f2er.com/jquery/428295.html

猜你在找的jQuery相关文章