2011-11-30 97 views
0

我想用jQuery的倒計時做兩件事情。我查看了延遲方法,但它不會應用於文檔中顯示的元素。非常簡單的jQuery倒計時

我想要做的就是等待2小時,然後在30秒超時之前做功能A,然後以後做功能B.

在功能AI會呈現出一個模式,我想顯示的消息說你有30秒的時間來回應。並顯示執行的函數B.

什麼會做這一切的最好,最簡單的方法前30倒計時?

謝謝

回答

1

如果您不希望代碼明顯過於混亂,請使用倒計時插件(例如jCounter)並運行它2小時,並設置其回退(回退A)以啓動另一個倒計時實例,持續30秒與第二次後備(後備B)。

實施例:

$('.countdown').jCounter({ 
    customDuration: 60*60*2, //2 hours 
    format: 'hh:ss', //display hours and seconds 
    fallback: function() { $('.timeLeft').jCounter({ // fallback A 
    customDuration: 30, // run for 30 seconds 
    format: 'ss', // display only seconds 
    fallback : function() { alert("30 seconds are up!") } // fallback B 
    }); } 
}); 
1

如果使用版本1.7或jQuery的1.7.1,我的建議是:

a) A simple timeout for the first countdowm (the 2 hour one); unless you 
    want a countdown displayed. 

b) for the 30 seconds, use the $.when feature to fire the second function: 

    $.when(count-down-reaches-zero-or-user-responds).done(function() { 
     B 
    }) 

如果你需要編寫一個簡單的顯示倒計時幫助,您可以在這裏的代碼來看看 (http://zequinha-bsb.int-domains.com/meetings.html