我在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
}}