gpt4 book ai didi

javascript - 捕获 setTimeout 的滴答

转载 作者:行者123 更新时间:2023-11-29 10:11:46 24 4
gpt4 key购买 nike

是否有可能在 setTimeout 中捕获每一秒的滴答声。假设我需要在 5 秒后注销用户并显示一条消息,例如在 div 中说“您将在 5 秒内注销”。然后是 4,3,2,1 并在 timeOut

后注销

例如:

setTimeout(function(){
$("#myDiv").text("you will be logged out in " + n + " seconds");
//"n" being tick of each second.
},5000)

我在互联网上搜索过此问题,但无处可寻。或者任何可以用 setInterval 为这个场景做的事情也是这里的好点。对此有什么想法或建议吗?

最佳答案

那么你可以使用 setInterval结合clearInterval方法:

var n = 5;
var timeoutID = window.setInterval(function() {
// this callback will execute every second until we call
// the clearInterval method
$('#myDiv').text('you will be logged out in ' + n + ' seconds');
n--;
if (n === 0) {
// TODO: go ahead and really log the dude out

window.clearInterval(timeoutID);
}
}, 1000);

关于javascript - 捕获 setTimeout 的滴答,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32159482/

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