在angularjs中导航期间的停止时间间隔
Stop time interval during navigation in angularjs
当前我使用的是angular js
我有一种在default.html
页面中从服务器端获取数据的方法
function bindActiveLoans() {
$http.get(rootUrlSingleEscaped + '/cooperativa/allunfundedloans/?authId=' + userInfo.UserId)
.success(function (response) {
if (response.Loans.length == 0) {
$scope.IsValues = false;
$scope.$apply();
return true;
}
$scope.IsValues = true;
$scope.unfundedLoans = response;
});
};
setInterval(function () {
$scope.$apply(bindActiveLoans());
}, 5000);
上述功能有助于每5秒从服务器端方法获取数据。
这里,我有一个问题。
另外,我还有一些页面,比如
default2.html、default3.html、default4.html、default5.html.
当我将
default.html
页面导航到default1.html
页面时,计时器仍在运行。我只想让计时器在default.html
页面中运行。如果我转到另一页,那么计时器应该停止。我们如何才能做到这一点?
来自$interval API页面中的angularjs示例。
var interval = $interval(function() {
console.log('say hello');
}, 1000);
$interval.cancel(interval);
使用$interval.cocancel(var),您可以停止间隔,如果您想在导航到另一个页面时停止它,您应该尝试这样的操作。
var interval = null;
module.controller('SomeController', '$interval', function($interval) {
interval = $interval(function() {
...
}, 2000);
}).run(['$rootScope', '$interval', function($rootScope, $interval) {
$rootScope.$on('$routeChangeStart', function() {
$interval.cancel(interval);
});
}]);
您可以监听$routeChangeStart
事件并根据路由参数清除间隔
$scope.$on('$routeChangeStart', function (scope, next, current) {
if(<next is not default.aspx>){
// clear interval here
}
});
从默认页面导航时调用clearInterval函数
例如
var myVar = setInterval(function () {
$scope.$apply(bindActiveLoans());
}, 5000);
function myStopFunction()
{
clearInterval(myVar);
}
调用myStopFunction
相关文章:
- Java脚本时间添加
- 单击顶部导航时如何进行向下滚动效果(向下滑动).
- angular 1.5应用程序中的导航栏
- Json数据包含日期和时间格式
- 无法从jquery Mobile导航栏重定向到另一个页面
- 如何根据时间运行不同的脚本
- 用每小时的差值填充数组/列表-从下拉列表中给定两个时间值
- 在wordpress一定时间后更改自定义字段
- 引导程序:在导航栏中,显示悬停在单个位置的基于Li Link的不同内容
- 导航到特定事件的另一个变量页面
- 更改angularjs中的日期-时间格式
- 将当前时间添加到我的页面上的特定部分
- 使用HTML5 Navigation Timing API时,导航开始时间设置为0
- 即使用户正在导航到其他页面,也要在一段时间后打开弹出窗口
- 在angularjs中导航期间的停止时间间隔
- fadeIn mousemove()上的导航-某个时间后fadeOut
- 如何将当前GPS导航器位置设置为destinationA的变量,以查找距离和时间
- CSS 时间轴滚动条禁用导航
- timeDone在RT插件中的差异在回飞镖vc导航计时页面加载时间
- 这个Silverlight时间导航控件有JavaScript/HTML版本吗?