我正在通过 ajax 调用加载的页面中执行 setTimeout 函数。但是如果我再次点击链接加载页面,恐怕最后的setTimeout调用仍然会继续并且setTimeout设置的调用间隔次数会执行多次。
试过这是远程页面:
var RefreshInterval = null;
clearTimeout(RefreshInterval);
function someFunction()
{
....
setNextRefresh();
}
function setNextRefresh() {
console.log(wifiRadarRefreshInterval);
RefreshInterval = null;
clearTimeout(RefreshInterval);
RefreshInterval = setTimeout('someFunction();', 20*1000);
}
最佳答案
在由 ajax 加载的页面之外声明 var RefreshInterval = null;
并在页面上使用此代码:
clearTimeout(RefreshInterval);
function someFunction()
{
....
setNextRefresh();
}
function setNextRefresh() {
console.log(wifiRadarRefreshInterval);
clearTimeout(RefreshInterval);
RefreshInterval = setTimeout('someFunction();', 20*1000);
}
关于Javascript setTimeout 在 ajax 加载的页面中触发多次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36080468/