gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-03 05:38:31 24 4
gpt4 key购买 nike

我想显示一些不同的时钟。下面给出的代码对我来说工作正常。但这次我想在用户需要时推送和播放。我想推送并恢复 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();

最佳答案

这是@colburton 和@TwiStar 的混合 react

正确回答问题

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();

关于javascript - 如何推送/播放 setTimeout 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24364219/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com