我不明白爲什麼clearInterval
不適合我。在間隔中,我以表格格式顯示二維數組,然後更新數組,並反覆重複。爲什麼我不能清除間隔?
爲什麼clearInterval
無法正常工作?
的JavaScript頭:
$(window).load(function() {
var tabl = document.getElementById('ordersTable');
var l = tabl.rows.length;
var w = tabl.rows[0].cells.length;
alert("Width:" + w);
var array1 = new Array(l);
var array2 = new Array(l);
initialize();
alert(JSON.stringify(array1));
var st = setInterval(function() {
display(array1);
newArray();
//alert(JSON.stringify(array1));
}, 3000);
function stopTime() {
clearInterval(st);
}
function display(array) {
for (var i = 0; i < l; i++) {
for (var j = 0; j < w; j++) {
var tr = tabl.rows[i];
var cll = tr.cells[j];
var ct = cll.innerHTML = array[i][j];
if (array[i][j] == 1) {
$(cll).css("color", "red");
} else {
$(cll).css("color", "black");
}
cll.id = array1[i][j];
}
}
}
function initialize() {
for (var i = 0; i < l; i++) {
array1[i] = new Array(w);
array2[i] = new Array(w);
for (var j = 0; j < w; j++) {
var rand = Math.floor(Math.random() * 2);
array1[i][j] = rand;
}
}
}
function newArray() {
for (var i = 0; i < l; i++) {
for (var j = 0; j < w; j++) {
var rand = Math.floor(Math.random() * 2);
array1[i][j] = rand;
}
}
}
});
HTML身體:
<table id="ordersTable">
<tr><td>Row 1</td><td> Row 1.2</td><td>Row 1.3</td></tr>
<tr><td>Row 2</td><td> Row 2.2</td><td>Row 2.3 </td></tr>
<tr><td>Row 3</td><td> Row 3.2 </td><td>Row 3.3</td></tr>
</table>
<button onclick="stopTime()">Stop</button>
動功的$上述ST =「」(窗口).load(函數(){ – Grumpy
@SLaks我upvoted您的評論但那只是壓痕差 - 這是不是一個可變的問題 –
@BenjaminGruenbaum感謝您修復這個問題! –