gpt4 book ai didi

javascript - 使用clearInterval方法后如何再次运行setInterval函数? - JavaScript

转载 作者:行者123 更新时间:2023-12-03 00:52:31 25 4
gpt4 key购买 nike

我有一个疑问......使用 clearInterval() 方法后是否可以再次运行 setInterval() 函数?

    var statistiche_recenti_storico = 0;
var orologio_statistiche_recenti;
orologio_statistiche_recenti = setInterval(function() {
if (statistiche_recenti_storico == 0) {
statistiche_recenti_storico = 1;
alert('end');
clearInterval(orologio_statistiche_recenti);
}
}, 5000);

$('#ok').on('click', function(){
// ????
});

我想在点击#ok后再次运行orologio_statistiche_recenti。可能吗?

此代码位于 ready() 事件 (JQuery) 内。

非常感谢,抱歉我的英语...

编辑这是一个 jsfiddle 示例:https://jsfiddle.net/tr9hw30a/

最佳答案

创建一个函数,并在代码中调用它,这是一种可行的方法。

我已将您的代码包装在 IIFE 中,这会阻止“vars”成为全局范围的一部分,您说您的代码位于 jQuery 就绪函数内,因此这对您来说不是必需的。

我定义了一个函数“startInterval”,它处理间隔的创建,它在脚本底部和点击处理程序中被调用。请注意,如果您不想在脚本运行后立即触发间隔,请删除脚本底部对 startInterval 的调用,只保留单击处理程序中的调用。

我还在 startInterval 函数中进行了检查,以清除正在运行的任何现有间隔,即停止重复。

// Wrapped in ann IIFE for scoping
(function () {

var statistiche_recenti_storico = 0;
var orologio_statistiche_recenti;

// Define a function which handles the creation of your interval
function startInterval () {

// Delete any existing interval
if (orologio_statistiche_recenti) {

// You could add a return here instead...
clearInterval(orologio_statistiche_recenti);
}

orologio_statistiche_recenti = setInterval(function() {
if (statistiche_recenti_storico == 0) {
statistiche_recenti_storico = 1;
alert('end');
clearInterval(orologio_statistiche_recenti);
}
}, 5000);
}


$('#ok').on('click', startInterval);

//Start your interval, by invoking the function
startInterval();

})();

完整示例如下。上述工作正常,由于您的 IF 语句,您的警报仅触发一次,该间隔实际上已被触发。

<!doctype html>
<html>
<body>

<button id="ok">OK</button>
<script
src="https://code.jquery.com/jquery-2.2.4.min.js"
integrity="sha256-BbhdlvQf/xTY9gja0Dq3HiwQF8LaCRTXxZKRutelT44="
crossorigin="anonymous">
</script>

<script>
// Wrapped in ann IIFE for scoping
(function () {

var statistiche_recenti_storico = 0;
var orologio_statistiche_recenti;

// Define a function which handles the creation of your interval
function startInterval () {

// Delete any existing interval
if (orologio_statistiche_recenti) {

// You could add a return here instead...
clearInterval(orologio_statistiche_recenti);
}


orologio_statistiche_recenti = setInterval(function() {

if (statistiche_recenti_storico == 0) {

// @BUG
// You were setting this to 1 after the first interval
// thus subsequent calls to this were not triggering the alert.

// statistiche_recenti_storico = 1;
alert('end');
clearInterval(orologio_statistiche_recenti);
}
}, 5000);
}


$('#ok').on('click', startInterval);

//Start your interval, by invoking the function
startInterval();

})();
</script>
</body>
</html>

关于javascript - 使用clearInterval方法后如何再次运行setInterval函数? - JavaScript,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52987902/

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