Node.js在脚本运行时n秒后调用一个函数

时间:2017-04-15 15:15:34

标签: node.js

我在node.js中有一个脚本,我想在另一个函数(或脚本的开头)启动后n秒调用我的end()函数。 所以我尝试使用setTimeout(function() {this.end()}, n);,但是setTimeout只是作为一个睡眠,这很糟糕,而不是我想要实现的。

情景:

class Myclass {

constructor() {
    this.doStuff();
}

doStuff() {
    //Does stuff until ended by end()
}

end() {
    //ends doStuff(), end() is usually called by the user but sometimes the user does not and I don't want doStuff() to run indefinitely so I need to kill it after n seconds
}}

0 个答案:

没有答案