有一种方法可以配置javascript的setInterval方法来立即执行该方法,然后与计时器一起执行


当前回答

在ES2017中,最好完全避免使用setInterval。

下面的解决方案具有更清晰的执行流程,可以防止函数需要比预期时间更长的时间才能完成的问题,并允许异步操作。

const timeout = (delayMs) => new Promise((res, _rej) => setTimeout(res, delayMs));

const DELAY = 1_000;

(async () => {
  while (true) {
    let start_time = Date.now();

    // insert code here...

    let end_time = Date.now();
    await timeout(DELAY - (end_time - start_time));
  }
})();

其他回答

你可以将setInterval()包装在提供该行为的函数中:

function instantGratification( fn, delay ) {
    fn();
    setInterval( fn, delay );
}

...然后这样使用它:

instantGratification( function() {
    console.log( 'invoked' );
}, 3000);

有一个方便的npm包叫做firstInterval(完全披露,它是我的)。

这里的许多示例都不包括参数处理,在任何大型项目中更改setInterval的默认行为都是有害的。从文档中可以看出:

这种模式

setInterval(callback, 1000, p1, p2);
callback(p1, p2);

等于

firstInterval(callback, 1000, p1, p2);

如果您使用的是老式浏览器,并且不想要依赖项,那么可以简单地从代码中剪切和粘贴。

对于那些使用React的人,下面是我解决这个问题的方法:

const intervalRef = useRef(0);

useEffect(() => {
    if (condition is true){
        if (intervalRef.current === 0) {
            callMyFunction();
        }
        const interval = setInterval(() => {
            callMyFunction();
        }, 5_000);
        intervalRef.current = interval;
    } else {
        clearInterval(intervalRef.current);
    }
}, [deps]);

我不确定我是否理解正确,但你可以很容易地做这样的事情:

setInterval(function hello() {
  console.log('world');
  return hello;
}(), 5000);

显然有很多方法可以做到这一点,但这是我能想到的最简洁的方法。

因为有人需要把外面的这个放在里面就像一个箭头函数一样。

(function f() {
    this.emit("...");
    setTimeout(f.bind(this), 1000);
}).bind(this)();

如果上面产生的垃圾困扰着你,你可以做一个闭包。

(that => {
    (function f() {
        that.emit("...");
        setTimeout(f, 1000);
    })();
})(this);

或者根据您的代码考虑使用@autobind装饰器。