我需要偶尔执行一些有效负载功能的功能,但需要考虑
答案 0 :(得分:2)
为了简单起见,只需使用新的ESnext async / await语法即可轻松实现。起初我们需要一个小帮手计时器:
const time = ms => new Promise(res => setTimeout(res, ms));
要像这样使用:
(async function(){
while(true){
await whatever(); // whatever shall be a promise
//wait some time:
await time(1000);
}
})()
答案 1 :(得分:0)
非常感谢https://www.thecodeship.com/web-development/alternative-to-javascript-evil-setinterval/以及我的小改进,我发布了这个解决方案,请随时纠正我。
type
tMyFooClass = class of tMyFoo;
tMyFoo = class
constructor Create; virtual;
end;
tMyFooDescendant = class(tMyFoo)
constructor Create(a: Integer); reintroduce;
end;
procedure .......
var
tmp: tMyFooClass;
begin
// Create tMyFooDescendant instance one way
tmp := tMyFooDescendant;
with tmp.Create do // please note no a: integer argument needed here
try
{ do something }
finally
free;
end;
// Create tMyFooDescendant instance the other way
with tMyFooDescendant.Create(20) do // a: integer argument IS needed here
try
{ do something }
finally
free;
end;
function interval(func, wait, times) {
var _interval = function () {
if (typeof times === "undefined" || times-- > 0) {
try {
Promise.resolve(func())
.then(() => { window.setTimeout(_interval, wait) });
}
catch (e) {
times = 0;
throw e.toString();
}
}
};
_interval();
return { stop: () => { times = 0 } };
};
返回带有interval()
字段的对象,因此您可以运行它来停止计时器,如:
stop