2016-09-06 56 views
1

由於計時器問題,我被卡住了。Ionic&AngularJS,超時未取消

簡而言之,代碼提交表單,並且在iFrame中打開對帖子的回答。 表單上的EventListener允許我檢測「提交」並應用計時器來檢測iFrame加載是否過長。 但是,即使加載了iFrame(=取消定時器),定時器的功能也會被調用。 下面是代碼:

[...] 
var el = document.getElementById("myForm"); 
if(el){ 
el.addEventListener("submit", function(event){ 
    $scope.popupOpened=false; 

    var iframeWindow = window.parent.document.getElementById('myIframe'); 
    var iframeDoc = iframeWindow.contentWindow.document; 

    //7 seconds before the popup is shown up 
    $rootScope.myTimer = $timeout(function(){ 
     window.frames[0].stop(); 
     if(!$scope.popupOpened){ 
     errorConnexion2();//popup opening 
     } 
    }, 7000); 

    iframeWindow.onload=function(){ 
    console.log("iframe loaded"); 
     //cancel the timer 
     $timeout.cancel($rootScope.myTimer); 
    } 
}); 
} 

[...]

我也試圖與setTimeout和clearTimeout並通過清除所有的人都這樣:Is there a way to clear all time outs?

我缺少什麼? 提前謝謝!

回答

0

好的,所以我找到了定時器沒有被取消的原因!

我嘗試調試,當提交表單時設置alert(),並發現程序在提交eventListener中多次輸入。 我雖然關於捕捉所有計時器ID並殺死它們,但我最終決定放棄並找到根本原因。

這個問題是在我看來,在.html頁面中我多次調用'ng-controller =「MyCtrl」'!所以,我剛剛刪除了額外的和程序工作!

希望它有幫助!