2013-11-27 23 views
1

我做了2次倒計時事件,但我無法在以後使用clearTimeout停止setTimeout(countDown)函數。這是我的jQuery:使用jQuery的javascript clearTimeout問題

$(document).ready(function() { 
var lives = 5 
var livesmenos1 = "4"; 
var livesmenos1 = 4; 
var countdownfinal = 10; 
var t; 
var isTimeron = false; 
var onelive = "1"; 
var onelive = 1; 

$(".wrongbutton, .wrongbutton2, .wrongbutton3").click(function() { 
    $(".countdownfirst").text(livesmenos1) 
    livesmenos1--; 
    if (livesmenos1 == 0) (livesmenos1 = 0); 
    if (livesmenos1 < 2) 
     $(".countdownfirst").css({ "color": "990099", "font-size": "380%" }); 
    if (livesmenos1 < 1) 
     $(".lives").attr("src", "images/morelives.gif"); 
    if (livesmenos1 < 0) 
     $("#tableone, #tabletwo").delay(1000).hide(2000); 
    if (livesmenos1 < 0) 
     $("#tableloser").delay(2000).fadeIn(2500, function countDown() { 
      setTimeout(countDown, 1000); 
      $("#box").text(countdownfinal + " seconds").css({ "color": "990099" }); 
      countdownfinal--; 
      if (countdownfinal < 1) (countdownfinal = 10); 
      if (countdownfinal < 2) 
       $(".countdownfirst").text(onelive++); 
      if (onelive > 5) 
       $(".countdownfirst").text("5"); 
      if (onelive > 1) 
       $("#nextbuttonloser").delay(1000).fadeIn(500).delay(30000); 
     }); 
}); 
}); 

然後我做了myStopFunction的onclick按鈕啓動clearTimeOut功能:

function myStopFunction() 
{ 
clearTimeout(countDown); 
} 

,它不工作。我試圖觸發也使用jQuery,但再次,沒有效果。我該如何停止名爲「countDown」的setTimeout函數?

感謝

+0

您不顯示'countDown'的定義位置,這非常相關。 –

回答

1

有沒有辦法從一個Timeout射擊停止特定的功能。唯一的方法是停止超時本身。每次超時都會在調用時返回一個整數值。使用此值來清除超時。請注意,它需要可用於您的功能範圍

var TimeoutHandle; 
$(document).ready(function() { 
... 
TimeoutHandle = setTimeout(countDown, 1000); 
... 
}); 

function myStopFunction() 
{ 
clearTimeout(TimeoutHandle); 
} 
+0

是的。我試圖創建一個30秒的延遲來停止計時器本身,並創建一個if stat等於零但是..是的。謝謝 – mpestana