【发布时间】:2016-01-11 09:16:57
【问题描述】:
Js 小提琴:check here
我在一个类的方法中设置了 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);
【问题讨论】:
标签: javascript jquery oop setinterval