gpt4 book ai didi

javascript - 如何停止间隔功能

转载 作者:行者123 更新时间:2023-11-28 11:35:50 25 4
gpt4 key购买 nike

我试图用 return 语法停止:

<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 秒验证一次表中是否存在数据。

data.exists == 1 (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>

关于javascript - 如何停止间隔功能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21311662/

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