Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/451.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用window.setTimeout()在JavaScript中编写递归方法?_Javascript_Recursion - Fatal编程技术网

如何使用window.setTimeout()在JavaScript中编写递归方法?

如何使用window.setTimeout()在JavaScript中编写递归方法?,javascript,recursion,Javascript,Recursion,我正在编写一个JavaSCript类,它有一个递归调用自身的方法 Scheduler.prototype.updateTimer = function () { document.write( this._currentTime ); this._currentTime -= 1000; // recursively calls itself this._updateUITimerHandler = window.setTimeout( arguments.cal

我正在编写一个JavaSCript类,它有一个递归调用自身的方法

Scheduler.prototype.updateTimer = function () {
    document.write( this._currentTime );
    this._currentTime -= 1000;
    // recursively calls itself
    this._updateUITimerHandler = window.setTimeout( arguments.callee , 1000 );
}
物业说明:

_currentTime: the currentTime of the timer in miliseconds.
_updateUITimerHandler: stores the reference so can be used later with clearTimeout().

我的问题是在哪里使用带有setTimeout的递归。我知道setTimeout将接受要执行的字符串或对函数的引用。由于这个函数是对象的方法,我不知道如何从外部调用它。因此,我使用了setTimeout的第二种格式,并传入了对方法本身的引用。但是它不起作用。

首先要说的是,如果调用setTimeout但不更改间隔,那么应该使用setInterval

EditUpdatefromComment:如果作为类使用,并且setInterval/clearInterval不需要重新引用,则可以保留闭包中的引用

edit2:有人指出,您编写了callee,它将非常正确地工作,并且100%明确无误

出于完整性考虑,这项工作:

function f() 
{
  alert('foo');
  window.setTimeout(arguments.callee,5000);
}

f();

所以我尝试了document.write而不是alert,这就是问题所在。doc.write充满了这样的问题,因为打开和关闭DOM进行编写,所以您可能需要更改目标的innerHTML,而不是doc.write。首先要说的是,如果您正在调用setTimeout,但没有更改间隔,那么您应该使用setInterval

EditUpdatefromComment:如果作为类使用,并且setInterval/clearInterval不需要重新引用,则可以保留闭包中的引用

edit2:有人指出,您编写了callee,它将非常正确地工作,并且100%明确无误

出于完整性考虑,这项工作:

function f() 
{
  alert('foo');
  window.setTimeout(arguments.callee,5000);
}

f();

所以我尝试了document.write而不是alert,这就是问题所在。doc.write充满了这样的问题,因为在编写时打开和关闭DOM,所以您可能需要更改目标的innerHTML,而不是doc.write,您可以将指针指向它

/* ... */
var func = arguments.callee;
this._updateUITimerHandler = window.setTimeout(function() { func(); }, 1000);
/* ... */

你可以拿一个指向它的指针

/* ... */
var func = arguments.callee;
this._updateUITimerHandler = window.setTimeout(function() { func(); }, 1000);
/* ... */
试试这个:-

Scheduler.prototype.startTimer = function() {
  var self = this;
  function updateTimer() {
    this._currentTime -= 1000;
    self.hTimer = window.setTimeout(updateTimer, 1000)
    self.tick()
  }
  this.hTimer = window.setTimeout(updateTimer, 1000)
}
Scheduler.prototype.stopTimer = function() {
    if (this.hTimer != null) window.clearTimeout(this.hTimer)
  this.hTimer = null;
}
Scheduler.prototype.tick = function() {
  //Do stuff on timer update
}
试试这个:-

Scheduler.prototype.startTimer = function() {
  var self = this;
  function updateTimer() {
    this._currentTime -= 1000;
    self.hTimer = window.setTimeout(updateTimer, 1000)
    self.tick()
  }
  this.hTimer = window.setTimeout(updateTimer, 1000)
}
Scheduler.prototype.stopTimer = function() {
    if (this.hTimer != null) window.clearTimeout(this.hTimer)
  this.hTimer = null;
}
Scheduler.prototype.tick = function() {
  //Do stuff on timer update
}

谢谢你的建议。使用setInterval是一个更好的选择。虽然因为我是以库的形式编写这个CALS,我不知道从我的类实例化的对象的名称。所以我不能在我自己的课堂上给他们打电话。你是不是把被叫人和被叫人搞混了?哈-没错,被叫人干得很好,我会修正的谢谢你的建议。使用setInterval是一个更好的选择。虽然因为我是以库的形式编写这个CALS,我不知道从我的类实例化的对象的名称。所以我不能在我自己的类中调用它们。你不是把被调用方和调用方搞混了吗?哈-没错,被调用方工作得很好,会修正的这不是递归的只是连续的这不是递归的只是连续的这不能解决访问适当实例成员的需要。后续func调用将使“this”指向窗口对象,而不是Scheduler的特定实例。这并不能解决访问相应实例成员的需要。随后的func调用将“this”指向窗口对象,而不是scheduler+1-@farzad的特定实例:我认为这也值得你投赞成票,而不仅仅是你接受的勾号-+1-@farzad:我认为这也值得你投赞成票,而不仅仅是你接受的选票-