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


当前回答

对于那些使用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]);

其他回答

函数的即时异步调用存在一个问题,因为标准的setTimeout/setInterval即使直接将其设置为0,也具有大约几毫秒的最小超时。这是由浏览器特定的工作引起的。

一个具有REAL零延迟的代码示例,可在Chrome, Safari, Opera中工作

function setZeroTimeout(callback) {
var channel = new MessageChannel();
channel.port1.onmessage = callback;
channel.port2.postMessage('');
}

你可以在这里找到更多信息

在第一次手动调用之后,您可以用函数创建一个间隔。

这个例子建立在@Alnitak的回答上,但是使用await Promise在循环循环中进行更细粒度的控制。

比较的例子:

let stillGoing = true;

(function foo() {
    console.log('The quick brown fox did its thing');
    if (stillGoing) setTimeout(foo, 5000);
})();

foo();

在上面的例子中,我们调用foo(),然后它每5秒调用一次自己。

但是如果,在未来的某个时候,我们为了停止循环而将stillGoing设为false,即使在发出停止命令之后,我们仍然会得到一个额外的log行。这是因为在任何给定的时间,在我们将stillGoing设置为false之前,当前迭代将已经创建了一个超时来调用下一次迭代。

如果我们使用await Promise作为延迟机制,那么我们就有机会在调用下一次迭代之前停止循环:

let stillGoing = true;

(async function foo() {
    console.log('The quick brown fox did its thing');
    await new Promise(resolve => setTimeout(resolve, 5000));
    if (stillGoing) foo();
})();

foo();

在第二个示例中,我们首先设置5000ms的延迟,之后检查stillGoing值并决定是否调用另一个递归是合适的。

如果我们在任意点将stillGoing设为false,在我们设置值之后就不会打印出额外的log行。

需要注意的是,这要求函数是异步的,对于给定的使用,这可能是也可能不是一个选项。

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

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

...然后这样使用它:

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

如果你可以使用RxJS,有一个叫做timer()的东西:

import { Subscription, timer } from 'rxjs';

const INITIAL_DELAY = 1;
const INTERVAL_DELAY = 10000;
const timerSubscription = timer(INITIAL_DELAY, INTERVAL_DELAY)
  .subscribe(() => {
    this.updateSomething();
  });

// when destroying
timerSubscription.unsubscribe();

我建议按以下顺序调用这些函数

var _timer = setInterval(foo, delay, params);
foo(params)

你也可以把_timer传递给foo,如果你想在特定的条件下清除interval (_timer)

var _timer = setInterval(function() { foo(_timer, params) }, delay);
foo(_timer, params);