1
下面的Javascript用於格式化我的PHP遊戲的一系列定時器。當用戶提交一個選項時,它會根據用戶提交的當前時間記錄的時間,重置定時器「準備就緒」以倒計時1分鐘。當計時器達到0時,它會重置爲默認的「就緒」。它與CHrome完美配合,但Firefox和IE只顯示Ready,但不會更新定時器並開始倒計時。任何幫助深表謝意。Mozilla和IE中的Javascript定時器倒計時 - 在Chrome中工作
var d = new Date();
var tarray = new Array();
function loadTimers()
{
var timersrow = g('timersrow');
var html = '';
var list = tinfo.split('|');
var i;
var cell
for (i=0; i<list.length; i++) {
data = list[i].split(',');
cell = ce('td');
cell.innerHTML = data[0];
timersrow.appendChild(cell);
//html += '<td id="tcell' + data[0] + '">' + data[0] + '</td>';
tarray[tarray.length] = new objTimer(data[0], data[1], cell);
}
//timersrow.innerHTML = html;
updateTimers();
}
function updateTimers() {
var i;
for (i=0; i<tarray.length; i++) {
tarray[i].update();
}
setTimeout('updateTimers();', 250);
}
function objTimer(label, time, cell)
{
this.label = label;
this.time = Date.parse(time);
this.cell = cell;
function update()
{
var t = new Date();
var val = this.time - t.getTime();
if (val > 0) {
this.cell.innerHTML = 'Next ' + this.label + ': ' + formatSeconds(val);
} else {
this.cell.innerHTML = 'Next ' + this.label + ': Ready';
}
}
this.update = update;
}
function formatSeconds(seconds)
{
var h = 0, m = 0,
seconds = parseInt(seconds/1000);
if (seconds > 60 * 60) {
h = parseInt(seconds/(60 * 60));
seconds -= h * 60 * 60;
}
if (h < 10) {
h = '0' + h;
}
if (seconds > 60) {
m = parseInt(seconds/60);
seconds -= m * 60;
}
if (m < 10) {
m = '0' + m;
}
if (seconds < 10) {
seconds = '0' + seconds;
}
return h + ':' + m + ':' + seconds;
}
loadTimers();
我們真的很喜歡jsfiddle/codepen :) – EricG
我同意,但有很多頁面和php變量等,使這個腳本工作。林希望有人可以看到我的JavaScript錯誤,這可能是爲什麼Firefox和ie不喜歡它的原因。除非我的日期格式 – Jarryn
只是爲了排除可能沒有錯誤的問題:在使用它們之前聲明你的函數,並將'radix'參數添加到''parseInt''調用中,像'parseInt(seconds/60,10)'..但這可能不會解決問題 – nozzleman