在javascript类中递归地设置超时调用
我的程序中有这些代码行。我想在javascript类中实现setTimeout。我试着储存这个,但似乎不适合我。 在这个类中,逻辑是如果输入为0,那么它将显示输出0,但是如果输入为1,它将启动一个已经指定的计时器,并且在该计时器完成后,它将给输出一个ondelay计时器在javascript类中递归地设置超时调用,javascript,Javascript,我的程序中有这些代码行。我想在javascript类中实现setTimeout。我试着储存这个,但似乎不适合我。 在这个类中,逻辑是如果输入为0,那么它将显示输出0,但是如果输入为1,它将启动一个已经指定的计时器,并且在该计时器完成后,它将给输出一个ondelay计时器 ONDClass = function(){ this.id; this.input; this.source; this.target; this.timer; var
ONDClass = function(){
this.id;
this.input;
this.source;
this.target;
this.timer;
var timerValue;
this.TimerID;
this.TotalSeconds;
var thisObj = this;
var c = 0;
this.setID = function(id){
this.id = id;
timerValue = this.id + "C";
}
this.getID = function(){
return this.id;
}
this.setTimer = function(timer){
this.timer = timer
}
this.getTimer = function(timer){
return this.timer;
}
this.UIelementTaget = function(target){
this.target = target;
}
this.UIelementSource = function(source){
this.source = source;
}
this.setInput = function(input){
this.input = input;
}
this.getInput = function(){
return this.input
}
this.UpdateTimer = function(){
console.log(c);
document.getElementById(timerValue).innerHTML = c;
}
this.createTimer = function(timerValue,time){
this.TimerID = document.getElementById(timerValue);
this.TotalSeconds = time;
// this.UpdateTimer();
setTimeout(function() {
document.getElementById(timerValue).innerHTML = c;
console.log(c);
thisObj.Tick();
}, 1000)
}
this.Tick = function(){
c++
//this.UpdateTimer();
if(c < this.TotalSeconds){
setTimeout(function() {
document.getElementById(timerValue).innerHTML = c;
console.log(c);
thisObj.Tick();
}, 1000)
} else {
return this.input;
}
}
this.getOutput = function(){
if(this.input == 0){
var htmltimer = "<div id = " + timerValue + " class = 'timerValue'>" + this.getTimer() + "</div>";
$("#" + this.id).append(htmltimer);
return this.input;
} else {
var htmltimer = "<div id = " + timerValue + " class = 'timerValue'></div>";
$("#" + this.id).append(htmltimer);
this.createTimer(timerValue,this.getTimer());
return this.input;
}
}
this.getUISourceElement = function(){
if(this.source == undefined)
return false;
else
return true;
}
this.addExtraDiv = function(){
var divinput = this.id +"I";
var divoutput = this.id +"O";
var htmlinput = "<div id = " + divinput + " class = 'inputBoxTimer'>" + this.getInput()+ "</div>";
var htmloutput = "<div id = '" + divoutput + "' class = 'outputBoxTimer'>" + this.getOutput() + "</div>";
$("#" + this.id).append(htmloutput);
$("#" + this.id).append(htmlinput);
}
}
问题在于setTimeout函数。它不是每次都调用,并且无论其1或0都会给我相同的输出。你的意思是没有调用thisObj.Tick方法吗?是的,没有调用thisObj.Tick方法。不要将第三个参数与setTimeout一起使用,并非所有浏览器都支持它。您将thisObj保存在一个闭包中,并且无论如何都不使用传递的值。另外,只发布最小的代码来显示错误,而不是无关的代码。您不需要在构造函数顶部声明属性,只需根据需要分配给它们。$+这个.id.append。。。可以是$this.append。。。。只有构造函数的名称应以大写字母开头。最后,记下您的当前位置,当TotalSeconds=0时,停止调用Tick.BTW,ondely计时器是什么?