的js小提琴:check here的setInterval工作不正常使用javascript哎呀
我的setInterval()一類的方法裏面。它在單個實例創建時正常工作,但在創建多個實例時失敗。當創建多個實例時,只有最後創建的實例工作和其他停止。
我的腳本如下:
function timer() {
this.ran = Math.floor(Math.random() * 100) + 1;
this.cls = this.ran + '_ocar';
this.div = '<div class="' + this.cls + '">' + this.cls + '</div>';
$('body').append(this.div);
this.run = function() {
thi = this;
thi.k = 0;
setInterval(function() {
console.log(thi.cls);
$('.' + thi.cls).html(thi.k++);
}, 1000);
}
}
one = new timer();
one.run();
setInterval(function() {
new timer().run();
}, 5000);
謝謝,這有效.. –
看你的小提問題對我來說更有意義.....; – Jai
你是什麼意思@Jii? –