[英]How to stop the setInterval using jQuery
I have a dropdown where I can select the time for the setInterval
. 我有一个下拉菜单,可以在其中选择
setInterval
的时间。
test.php: test.php:
<select id="proc_id" class="multiselect custom processselect" name="proc_id" style="display: none;">
<option>Select</option>
<option value="0">0</option>
<option value="1">1</option>
<option value="2">2</option>
</select>
If select "0" stop the executing the setinterval
and 1 for 1 sec and 2 for 2 sec. 如果选择“ 0”,则停止执行设置
setinterval
,1表示1秒钟,2表示2秒钟。
test.js test.js
var set_time = 0;
var interval;
$('#proc_id').change(function() {
set_time = $('#proc_id :selected').val();
// alert((set_time));
if(parseInt(set_time) > 0) {
set_time= set_time * 1000;
interval = setInterval(function() {
getFileProcessList(start_date);
getResultInfoList(start_date);
}, set_time);
}
else {
alert('bye');
clearInterval(interval);
}
});
If I selected the 0
, it not clearing the interval. 如果我选择
0
,则不会清除间隔。 Please can anyone help how to stop the setInterval
function? 任何人都可以帮助停止
setInterval
函数吗?
You should clear the previous interval before start the new one. 您应该在开始新间隔之前清除上一个间隔。 Your code should be updated like this
您的代码应像这样更新
var set_time = 0;
var interval = 0;
$('#proc_id').change(function(){
set_time=$('#proc_id :selected').val();
if(parseInt(set_time) > 0) {
set_time= set_time * 1000;
if (interval != 0) {
// clear previous interval before start the new one
clearInterval(interval);
}
interval = setInterval(function() {
getFileProcessList(start_date);
getResultInfoList(start_date);
}, set_time);
}
else {
clearInterval(interval);
interval = 0;
}
});
I've created a sample at this link https://jsfiddle.net/mhL907eo/1/ , please check. 我已在此链接https://jsfiddle.net/mhL907eo/1/中创建了一个示例,请检查。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.