function scan (id) {
$http({
method: "POST",
url:( "url"),
headers: {'Content-Type': 'application/x-www-form-urlencoded'},
data: $.param({id: id}),
})
.then(scanComplete)
.catch(function(message) {
throw new Error("XHR Failed ",message);
});
function scanComplete(response, status, headers, config) {
//do something , show errors, popup , call to other functions
}
setTimeout(function() {scan())}, 1000);
}
这是我轮询的功能,例如检查数据库中是否有新通知。
我根本没有清除超时,也不使用 $timeout 。是不是错了?是否会导致 chrome 问题,如卡住或 CPU 开销?
我怎样才能清除,我怎样才能检查我是否真的清除了它
所有功能和用户界面方面。来自没有 Controller 的 Angular 服务/工厂(除了服务调用)
最佳答案
只需要在scanComplete
函数中再次调用
function scanComplete(response, status, headers, config) {
//do something , show errors, popup , call to other functions
setTimeout( scan, 1000); //invoke after your ajax is complete
}
这将确保您在任何给定时间点仅触发一次扫描
。
I don't clear the timeout at all and don't use the $timeout . is it mistake ? is it cause to chrome problem like freezing or CPU overhead?
由于您在任何时间点都只触发一次扫描
,而且在第一个扫描完成后也是如此,我看不出这将如何产生任何 CPU 开销。
关于javascript - 清除javascript或angular中的轮询超时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47976066/