在我的一個應用程序中,基於角度的應用程序在用戶閒置10分鐘後寫入用戶會話時間,在第5分鐘彈出警告,10分鐘後需要註銷。爲什麼無法在JavaScript中清除SetInterval?這種做法是錯誤的?
以下代碼適用於應用程序。 在一個模塊必須清除setinterval,我無法通過使用 **clearInterval(idleCheck);**
.so在下面的代碼中出錯。
角運行塊已經實現了用戶在狀態之間導航,延長了10分鐘的時間。
myApp.run(function ($rootScope $interval, $timeout, $state) {
$rootScope.$on('$stateChangeStart', function (event, toState, toParams, fromState, fromParams, $rootScope, $state, $location) {
lastDigestRun = Date.now();
var m = lastDigestRun + 10 * 60 * 1000;
idleCheck = setInterval(function() {
var now = Date.now();
if (now - lastDigestRun > 5 * 60 * 1000) {
// show the pop up , the pop have continue to extend the time session if proceed
}
if (now - lastDigestRun > 10 * 60 * 1000) {
// LOgout functionality
}
}
}, 60 * 1000);
});
});
如果用戶有一些服務器請求意味着在這個塊中有延長時間再加上時間。
我必須清除會話,如果url是這樣的;試圖清除它不清除。
if (url == 'stopsession') {
clearInterval(idleCheck);
}
var configFunction = function ($stateProvider, $httpProvider, $urlRouterProvider, $locationProvider,$scope) {
var interceptor = function ($q, $rootScope, $timeout, $location) {
return {
request: function (config) {
// consider this module reuest come here
if (url == 'stopsessionurl') {
clearInterval(idleCheck);
}
其他{
lastDigestRun = Date.now();
var m = lastDigestRun + 10 * 60 * 1000;
idleCheck = setInterval(function() {
var now = Date.now();
if (now - lastDigestRun > 5 * 60 * 1000) {
// show pop up to extend the time if click continue another 10 minutes gets added
}
if (now - lastDigestRun > 10 * 60 * 1000) {
// logout functionality
}
}, 60 * 1000);
return config;
}
},
requestError: function (config) {
return config;
},
response: function (response) {
},
responseError: function (rejection) {
}
}
};
$httpProvider.interceptors.push(interceptor);
};
所以在一個模塊必須停止setInterval.But ClearInterval沒有停止。
這是完整的一段代碼。
var idleCheck;
var myApp = angular.module('myApp','ui.router');
myApp.run(function ($rootScope $interval, $timeout, $state) {
$rootScope.$on('$stateChangeStart', function (event, toState, toParams, fromState, fromParams, $rootScope, $state, $location) {
lastDigestRun = Date.now();
var m = lastDigestRun + 10 * 60 * 1000;
idleCheck = setInterval(function() {
var now = Date.now();
if (now - lastDigestRun > 5 * 60 * 1000) {
// show the pop up , the pop have continue to extend the time session.
}
if (now - lastDigestRun > 10 * 60 * 1000) {
// LOgout functionality
}
}
}, 60 * 1000);
});
});
var configFunction = function ($stateProvider, $httpProvider, $urlRouterProvider, $locationProvider,$scope) {
var interceptor = function ($q, $rootScope, $timeout, $location) {
return {
request: function (config) {
if (url == 'stopsession') {
clearInterval(idleCheck);
}
else {
lastDigestRun = Date.now();
var m = lastDigestRun + 10 * 60 * 1000;
idleCheck = setInterval(function() {
var now = Date.now();
if (now - lastDigestRun > 5 * 60 * 1000) {
// show pop up to extend the time if click continue another 10 minutes gets added
}
if (now - lastDigestRun > 10 * 60 * 1000) {
// logout functionality
}
}, 60 * 1000);
return config;
}
},
requestError: function (config) {
return config;
},
response: function (response) {
},
responseError: function (rejection) {
}
}
};
$httpProvider.interceptors.push(interceptor);
};
問題:
但無法清除的時間間隔。
如果我使用$間隔,時間不正確,無法通過使用$ interval.cancel Github的工作,明確:演示:https://github.com/MohamedSahir/UserSession
步驟:經過視頻獲得更多有關的問題: 演示視頻: http://recordit.co/xHkJeUSdp1 演示plunker:https://plnkr.co/edit/UkVUvFWIQKYD6SUumNv4?p=preview
如果我第一次使用$ interval的方式,計算工作正常,當我再次延長時間,因爲計算錯誤,在它的註銷時間中間,如果我使用setInterval時間方式正常工作,但唯一的問題是與cleartimeout。如果您需要在某個時間之後創建一個plunker演示程序。 –
@MohamedSahir,請你能創建一個[最小,完整和可驗證的例子](http:// stackoverflow。com/help/mcve),可能是一個plunker或jsfiddle的最小代碼,所以我可以重現你的確切問題? – lealceldeiro
演示錄製的視頻:http://recordit.co/xHkJeUSdp1在這裏創建了github:https://github.com/MohamedSahir/UserSession,Plunker Here:https://plnkr.co/edit/3ywUgI9362UUatExZ4FM?p = preview我幫助,如果你發現了什麼 –