2017-05-02 37 views
0

工作,我有一個RightSidebarLayout頁在我的角度應用,我在那裏設置的間隔setInterval並在其上調用的函數,從工廠函數得到一個通知。

的問題是,我想清楚了它的用戶註銷之後。

這裏是我的代碼RightSidebarLayout.Controller.js

我想的第一件事是,我用普通的JavaScript中使用setInterval的方法,但我得到了ToastInterval not defined

var vm = this; 
function init(){ 
    var ToastInterval = setInterval(getAllNotifications, 9000); // when user has logged out stop my interval 
     } 
init(); 

function getAllNotifications() { 
    // alert(commonApi.IsUserLoggedIn()) // alerts true if logged in else false //working properly 
    if (!IsUserLoggedIn()){ 
     clearInterval(ToastInterval); // This shows error ToastInterval is undefined as i have defined it in parent environment 
      return; 
    } 
    else { 
     notificationService.getNoti() // Call a service and get all notification 
    } 
    } 

錯誤我在這裏的問題是,爲什麼我不得到ToastInterval,因爲它是在其lexical environment ,,,

2)所以我用我的ControllerAs$Interval CA定義聲明llback,它不會給我的錯誤,還是其沒有工作,但它仍然觸發功能

var vm = this; 
function init(){ 
    vm.ToastInterval = $interval(getAllNotifications, 9000); // when user has logged out stop my interval 
} 
init(); 

function getAllNotifications() { 
    // alert(commonApi.IsUserLoggedIn()) // alerts true if logged in else false //working properly 
    if (!IsUserLoggedIn()){ 
     $interval.cancel(vm.ToastInterval); 
     return; 
    } 
    else { 
     notificationService.getNoti() // Call a service and get all notification 
     } 
    } 

這裏即使我把我的時間間隔不清除警報的間隔也不會被清除..

什麼我做錯了?

請幫幫忙,

+0

但它是我的詞彙環境..爲什麼我的第二件事情不起作用 –

+1

您收到錯誤'ToastInterval not defined',因爲它是在'init'函數中定義的。聲明變量'ToastInterval',其中變量'vm'被聲明並且你的代碼將工作。 –

+0

是的,我的第二個代碼,我的虛擬機是全局的,然後也不工作 –

回答

-1

更改第一代碼

JS:

var vm = this, 
     ToastInterval; 
    function init(){ 
    ToastInterval = setInterval(getAllNotifications, 9000); // when user has logged out stop my interval 
    } 
    init(); 

    function getAllNotifications() { 
     // alert(commonApi.IsUserLoggedIn()) // alerts true if logged in else false //working properly 
      if (!IsUserLoggedIn()){ 
       clearInterval(ToastInterval); // This shows error ToastInterval is undefined as i have defined it in parent environment 
       return; 
      } 
      else { 
       notificationService.getNoti() // Call a service and get all notification 
      } 
     } 

我已經decalred初始化函數外的變量ToastInterval

+0

仍然無法運行我的setInterval仍未清除 –

+0

您能否提供一個小提琴,以便它可以進一步調試。 –

+0

它的工作,但我仍然沒有得到,爲什麼我這個,變量沒有工作 –