运行 如何在使用 clearInterval 方法后再次调用 setInterval 函数? - Javascript
How run again a setInterval function after used clearInterval method ? - Javascript
我有疑问....
在使用 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/
var statistiche_recenti_storico = 0;
var orologio_statistiche_recenti;
var intervalFn=function() {
if (statistiche_recenti_storico == 0) {
statistiche_recenti_storico = 1;
alert('end');
clearInterval(orologio_statistiche_recenti);
}
}
orologio_statistiche_recenti = setInterval(intervalFn, 5000);
$('#ok').on('click', function(){
// ????
setInterval(intervalFn, 5000);
});
将其作为一个单独的函数,然后在任何需要的地方使用 setInterval(functionName, interval)
再次调用。
//ogni secondo lancio questo codice con il setInterval
var statistiche_recenti_storico = 0;
var orologio_statistiche_recenti;
var startTimeout = function() {
if (orologio_statistiche_recenti) {
clearTimeout(orologio_statistiche_recenti); //interrompo questo setInterval
}
orologio_statistiche_recenti = setTimeout(function() {
console.log('timeout executed');
if (statistiche_recenti_storico == 0) {
statistiche_recenti_storico = 1;
alert('end');
}
}, 5000);
}
startTimeout();
$('#ok').on('click', startTimeout);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="ok">OK</div>
实际上您似乎想要超时而不是间隔。你在第一次执行后停止它。
创建一个函数,并在您的代码中调用它,这是一种可行的方法。
我已经将你的代码包装在一个 IIFE 中,这阻止了 'vars' 成为全局范围的一部分,你说你的代码在 jQuery 就绪函数中,所以这不是必需的给你。
我定义了一个函数 'startInterval',它处理间隔的创建,它在脚本底部和点击处理程序中被调用。请注意,如果您不想在脚本 运行 后立即触发间隔,请删除脚本底部对 startInterval 的调用,仅将调用保留在点击处理程序中。
我还检查了 startInterval 函数以清除任何现有的 运行ning 间隔,即停止重复。
// 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>
我有疑问....
在使用 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/
var statistiche_recenti_storico = 0;
var orologio_statistiche_recenti;
var intervalFn=function() {
if (statistiche_recenti_storico == 0) {
statistiche_recenti_storico = 1;
alert('end');
clearInterval(orologio_statistiche_recenti);
}
}
orologio_statistiche_recenti = setInterval(intervalFn, 5000);
$('#ok').on('click', function(){
// ????
setInterval(intervalFn, 5000);
});
将其作为一个单独的函数,然后在任何需要的地方使用 setInterval(functionName, interval)
再次调用。
//ogni secondo lancio questo codice con il setInterval
var statistiche_recenti_storico = 0;
var orologio_statistiche_recenti;
var startTimeout = function() {
if (orologio_statistiche_recenti) {
clearTimeout(orologio_statistiche_recenti); //interrompo questo setInterval
}
orologio_statistiche_recenti = setTimeout(function() {
console.log('timeout executed');
if (statistiche_recenti_storico == 0) {
statistiche_recenti_storico = 1;
alert('end');
}
}, 5000);
}
startTimeout();
$('#ok').on('click', startTimeout);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="ok">OK</div>
实际上您似乎想要超时而不是间隔。你在第一次执行后停止它。
创建一个函数,并在您的代码中调用它,这是一种可行的方法。
我已经将你的代码包装在一个 IIFE 中,这阻止了 'vars' 成为全局范围的一部分,你说你的代码在 jQuery 就绪函数中,所以这不是必需的给你。
我定义了一个函数 'startInterval',它处理间隔的创建,它在脚本底部和点击处理程序中被调用。请注意,如果您不想在脚本 运行 后立即触发间隔,请删除脚本底部对 startInterval 的调用,仅将调用保留在点击处理程序中。
我还检查了 startInterval 函数以清除任何现有的 运行ning 间隔,即停止重复。
// 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>