下面的 Javascript 用于为我的 php 游戏格式化一系列计时器。当用户提交一个选项时,它会重置计时器,说“准备好”,根据用户提交时记录的时间与当前时间倒计时 1 分钟。当计时器达到“0”时,它会重置为默认的“就绪”。它与 CHrome 完美配合,但 Firefox 和 IE 仅显示就绪,但不会更新计时器并开始倒计时。任何帮助都深表感谢。
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();