我正在运行一个函数来检查数据库条目是否存在。clearInterval在一种情况下失败,但在另一种情况下失败
在我的网页加载我检查元素是否存在,如果是这样我用setInterval
运行功能。像这样:
if ($('#encoding').length) {
console.log("auto_update start");
var update = setInterval(auto_update, 12000);
}
然后在我auto_update
功能发生这种情况
function auto_update() {
console.log("auto_update running");
$.ajax({
type: 'POST',
url: ajaxurl,
data: {
action: "dhf_ajax_router",
perform: "dhf_check_status", // the function we want to call
post_id: $('#post_id').val(),
nonce: $('#dhf-video-meta-nonce').val()
},
success: function(response) {
if (response === "continue") {
console.log("still encoding");
} else {
clearInterval(update);
console.log("complete " + response);
}
}
});
}
的问题是,如果$('#encoding')
不存在在开始页面上,并通过用户手动内触发:
$(document).on("click", ".run_encode", function(){
// doing the necessary stuff here.
if (response === "sent") {
// more stuff
var update = setInterval(auto_update, 12000);
}
}); // end .run_encode
然后clearInterval(update)
不起作用,它结束了一个无限循环。
我想不出为什么。在两种情况下都设置了名称为update
的间隔,那么为什么在第二种情况下清除它不起作用?
关键词:“JavaScript的变量范围” – 2012-08-10 07:23:13