2017-09-17 1 views
0

Am mit einem Countdown-Timer arbeiten, und ich will es stoppen, wenn 0 ist, ist hier mein CodeWie Timer Countdown stoppen

function countdown() { 
    // your code goes here 
    var count = $('.c').attr('id'); 
    var timerId = setInterval(function() { 
     count--; 
     $('.c').text(count) 
     if (count == 0) { 
      $("#page").load("page.php"); 
      $('#beforeloading').html(''); 
      count = 60; 
     } 
    }, 1000); 
} 

 

+0

Sie können 'clearInterval (intervalName)' – Keatinge

+0

Sie wirklich Ihre erste 'count' von einem' id' des Elements erhalten? –

+1

Mögliches Duplikat von [Stop setInterval Aufruf in JavaScript] (https://stackoverflow.com/questions/109086/stop-setinterval-call-in-javascript) – evilReiko

Antwort

0

Sie würden das Intervall innerhalb der Bedingung löschen

function countdown() { 
    var count = $('.c').attr('id'); 
    var timerId = setInterval(function() { 
    count--; 
    $('.c').text(count) 
    if (count == 0) { 
     $("#page").load("page.php"); 
     $('#beforeloading').html(''); 

     clearInterval(timerId); // here 
    } 
    }, 1000); 
} 
+0

Danke für die Antwort. – Iamgisnet

0

Ich glaube, Sie suchen clearInterval:

if (count == 0) { 
     $("#page").load("page.php"); 
     $('#beforeloading').html(''); 
     count = 60; 
     clearInterval(timerId); 
    } 
0

var Counter=1; 
 
var myInterval= setInterval(function(){ 
 
    $("span").text(Counter); 
 
    Counter++; 
 
    if(Counter==11){ 
 
    clearInterval(myInterval); 
 
     $("span").append(" Stop"); 
 
    } 
 

 
}, 1000);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<h4>Stop After 10s</h4><br> 
 
<span></span>

+0

Danke für die Antwort. Gute Arbeit – Iamgisnet

+0

Gern geschehen. Hoffe, nützlich zu sein. –