我想在我的PHP頁面上有一個與特定按鈕相關的倒計時,並且我使用的是基於javascript的以下代碼: 但是,它會重置頁面重裝上的目標值,所以如何擁有相同的目標值得到重置。我可以在會話中做些什麼?使用PHP的Javascript倒數
<html>
<body>
<p>Time remaining: <span id="countdownTimer"><span>00:00.<small>00</small></span></p>
<script type="text/javascript">
if (document.getElementById('countdownTimer')) {
pad = function(n, len) { // leading 0's
var s = n.toString();
return (new Array((len - s.length + 1)).join('0')) + s;
};
function countDown() {
var now = new Date();
if ((now.getDay() >= 0) && (now.getDay() <= 6)) { // Monday to Friday only
var target = 23; // 15:00hrs is the cut-off point
if (now.getHours() < target) { // don't do anything if we're past the cut-off point
var hrs = (target - 1) - now.getHours();
if (hrs < 0) hrs = 0;
var mins = 59 - now.getMinutes();
if (mins < 0) mins = 0;
var secs = 59 - now.getSeconds();
if (secs < 0) secs = 0;
var str = pad(hrs, 2) + ':' + pad(mins, 2) + '.<small>' + pad(secs, 2) + '</small>';
document.getElementById('countdownTimer').innerHTML = str;
}
}
}
var timerRunning = setInterval('countDown()', 1000);
}
</script>
</body>
</html>
你要正確地永久存儲或將瞬態行嗎?會話將起作用,就像客戶端上的一個簡單的cookie一樣。 – 2013-03-11 14:54:54
您沒有正確關閉您的'countdownTimer'範圍。永遠不要傳遞一個字符串到'setInterval',而是使用一個函數引用(在你的情況下:'setInterval(countDown,1000)')。 – 2013-03-11 14:56:38
我想持久存儲,但如何解決會議? – Ankur 2013-03-11 15:05:44