AngularJS:为什么不让我$interval stop ?
AngularJS: Why won't me $interval stop?
我已经看了文档,但现在是凌晨3点,我的课快结束了。这是我的控制器:
controller('makeDashCtrl', function ($scope, $rootScope, $cookies, $location, $http, $interval) {
var userId = $cookies.get('user_id');
var orgId = $cookies.get('org_id');
$http.get('/api/consolidateProfile/').then(function(data){
console.log(data);
}, function(res){
console.log(res + " - Eh, consolidateProfile probably timed out, no biggie")
})
var testStart = $interval(function(){
$http.get('/api/testProgress/' + userId).then(function(obj){
$scope.message = obj.data.message;
$scope.sub = obj.data.sub;
if(obj.data.nextPhase){
console.log("Should be cancelling...");
nextScrape();
$interval.cancel(testStart); // This one cancels fine
}
}, function(err){
$interval.cancel(testStart);
});
}, 1000);
function nextScrape(){
console.log("In checkScraperadsfsdfads!")
var checkScraper = $interval(function(){
console.log("In checkScraper REAL!")
$http.get('/api/checkScraper/' + userId).then(function(obj){
var msg = JSON.parse(obj.data);
console.log("Got some data!")
console.log(obj);
if(msg.doneFbs){
$scope.fbMsg = "We've finished gathering " + msg.doneFbs + " Facebook accounts";
}
if(msg.doneTwits){
$scope.twitMsg = "We've finished gathering " + msg.doneTwits + " Twitter accounts";
}
$scope.message = msg.message;
$scope.sub = msg.sub;
if(msg.finished){
$interval.cancel(checkScraper); // This does NOT cancel
console.log("Scraping Done!")
$location.path('/dashboard') // This successfully redirects to the next page
}
},function(err){
console.log("There was an error in Checkscraper ")
console.log(err)
$interval.cancel(checkScraper); // This does NOT cancel when there's an error
});
}, 3000)
}
})
请参阅上面代码中的注释。也许这是nextScrape()
函数的问题,但我不能让$interval
取消。即使当$location.path()
切换到下一页时,间隔仍在运行。
我在这里做错了什么?
如果调用的返回时间超过1秒,则可能同时有多个ajax请求。不使用$interval
,为什么不进行一次调用,并在第一次调用完成时使用$tiemout
来安排第二个调用?
function checkProgress() {
$http.get('/api/testProgress/' + userId).then(function(obj){
$scope.message = obj.data.message;
$scope.sub = obj.data.sub;
if(obj.data.nextPhase){
// move on to next phase
nextScrape();
} else {
// not done, schedule another check
$timeout(checkProgress, 1000);
}
}, function(err){
// error, you cancel, but maybe schedule another progress check?
});
}
我怀疑代码在收到Ajax的响应之前进行了多次调用。这意味着ajax的响应时间超过一秒钟。发生这种情况是因为您提到了从服务器再次池化数据的时间非常少。
要在页面重定向后停用$interval
,可以在scope
上使用$destroy
事件。事件需要手动清除,因为除非我们分离它们,否则它们不会消失。您可以在离开页面时停止interval。销毁作用域event
$scope.$on('$destroy', function(){
$interval.cancel(testStart);
})
相关文章:
- 如何抓住'stop'事件,然后做点什么
- Angular js$Interval怪异行为-Firefox Chrome
- 如何在aws S3 javascript SDK中配置interval和max_attempts
- $interval不允许我更新前端
- 正在尝试停止函数上的Interval
- 如何清除此代码中的Interval()
- 如何在量角器测试中设置$interval而不是超时
- 在源节点上调用 stop() 是否会触发已结束的事件
- Stop ::-webkit-progress-value animation
- Jquery可拖动&可丢弃事件委派:stop&滴
- 如何在react组件构造函数中设置Interval
- .stop()会破坏渐变元,而黄色框不会'看起来不太合适
- 函数中的Interval被调用数百次
- 如何让 .stop 工作并确保标题在我单击时停止,而不是在我单击的位置停止
- jquery stop() 无法正常工作
- JsFiddle:未知提供商:$intervalProvider <- $interval
- 当使用 Jasmine 对我的 angularJS 应用程序进行单元测试时,$interval是未定义的
- e.stop需要传播以防止点击事件冒泡到父级
- 滑动面板 .stop() 在 $(this).next 上不起作用
- AngularJS:为什么不让我$interval stop ?