清除计时器时运行SetTimeout的内容



我有超时:

clockifyButton.popupTimer = setTimeout(() => {
popup.style.display = "none";
aBrowser.runtime.sendMessage(
{
eventName: "startWithDescription",
description,
subject: title,
tracker,
taskid,
tag,
projectId: clockifyButton.selectedProject.id
},
response => {
if (response.code === 400) {
alert(
"Can't end entry without project/task/description or tags.Please edit your time entry."
);
} else {
active = true;
setButtonProperties(button, title, active);
clockifyButton.inProgressDescription = title;
aBrowser.storage.sync.set({
timeEntryInProgress: response.data
});
}
}
);
}, 5000);

当我从另一个源清除Timeout(popupTimer(时,我正在寻找如何发送消息的解决方案。谢谢转发!!!

取消超时将防止触发回调,因此您需要在计时器取消时自己调用它。

试试这样的东西:

class CancelableWait {
constructor(func, delay) {
this.func = func;
this._timer = setTimeout(() => this.cancel(), delay);
}
cancel() {
if (this._timer) {
clearTimeout(this._timer);
this._timer = null;
this.func();
}
}
}
console.log("Starting 5 second timer..");
let waiter = new CancelableWait(() => console.log("Fired!"), 5000);
setTimeout(() => {
console.log("Cancelling..");
waiter.cancel();
}, 2000);

无论是由于定时器的自然结束,还是由于对cancel的显式调用,这都只会触发回调一次。

相关内容

  • 没有找到相关文章

最新更新