如何停止间隔功能 [英] How to stop interval function
问题描述
I'm trying to stop with return
syntax:
我试图用 return
<脚本>
$(document).ready(function(){
setInterval(function(){
var url =../view/anychange.php;
$ .getJSON (url,function(data){
f(data.exists == 0){
alert(0);
} else {
alert(1);
return;
}
});
},5000);
});
< / script>
<script>
$(document).ready(function() {
setInterval(function() {
var url = "../view/anychange.php";
$.getJSON(url, function(data) {
f(data.exists == 0){
alert("0");
} else {
alert("1");
return;
}
});
}, 5000);
});
</script>
如果我的表中存在数据,函数会每5秒验证一次。
The function verifies every 5 seconds if there exists data in my table.
当 data.exists == 1
时( alert(1)),我需要停止该函数。 code>)。
I need to stop the function when data.exists == 1
( the alert("1")
).
推荐答案
<script>
$(document).ready(function() {
var id;
id = setInterval(function() {
var idRefCopy = id; // You need this otherwise you'll get a reference exception
var url = "../view/anychange.php";
$.getJSON(url, function(data) {
if(data.exists == 0){
alert("0");
} else {
alert("1");
clearInterval(idRefCopy);
return;
}
});
}, 5000);
});
</script>
这篇关于如何停止间隔功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!