2016-12-06 4 views
1

角度js $ timeout関数を使用して質問のスライドを作成しようとしていますが、実装できました。スライドをリフレッシュするように再起動することができます。他のスライドはタイムアウト遅延時間で動作しないため、リフレッシュは最初のスライドのみに適用されます。 ここに私のコード、メイン関数とリコール関数があります。

$scope.callTime = function() { 
     q_len = questions.length; 
      $timeout(function() { 
       //checking if the question index is still valid 
       if (q_indy < q_len) { 
        slides = questions[q_indy].pictures; 
        len = slides.length; 
        console.log(indy); 
        $scope.currentQuestion = questions[q_indy]; 
        f_time = parseInt(slides[indy].time_frame); 
        //getting the specific time needed to run this particular slide 
        r_time = f_time - initialTime; 
        if (indy < len) { 
         f_time = parseInt(slides[indy].time_frame); 
         var interval = slides[indy].picture_url; 
         indy++; 
         console.log(q_indy + " " + indy + " " + r_time); 
         //changing to the current picture so it can run for the given time 
         $scope.image = $scope.url.url + interval; 
         if (indy == len) { 
          //checking if the question's slides is out of index so the next question should be loaded 
          q_indy++; 
          indy = 0; 
          initialTime = f_time; 
          console.log(q_indy + " " + indy + " " + r_time); 
          $scope.callTime(); 
         } 
         else { 
          //if the slide index is active the next slide should be loaded then 
          initialTime = f_time; 
          $scope.callTime(); 
         } 
         //console.log($scope.url.url + interval); 
        } 
       } 
      }, r_time); 
      //time(); 
     } 
    $scope.refresh = function() { 
     $timeout.cancel($scope.callTime); 
     q_indy = 0; 
     indy = 0; 
     initialTime = 0; 
     r_time = 0; 
     len = 0; 
     q_len = 0; 
     $scope.callTime(); 
     var e = document.getElementById('myTune'); 
     //e.pause(); 
     e.currentTime = 0; 
     //e.play; 
    }; 

答えて

1

はあなたが$timeoutを呼び出したときに返された値を保存し、$timeout.cancel()にその値を渡す必要があります。代わりに、$timeout.cancel()が何かについて知っているものではないタイムアウトを作成するために使用した関数を渡しています。

var timer = null; 
$scope.callTime = function() { 
    q_len = questions.length; 
    $timeout.cancel(timer); // You probably also want to cancel here 
    timer = $timeout(function() { 
      // ... rest of your code here ... 
     }, r_time); 
     //time(); 
    } 
$scope.refresh = function() { 
    $timeout.cancel(timer); 
    // ... and rest of your code here ... 
}; 
+0

ありがとうございます...これはうまくいきました。私はタイマー変数にタイムアウト関数をラップしていました – itsdenty

関連する問題