Home > database >  How to implement setInterval() function with interval as Minutes
How to implement setInterval() function with interval as Minutes

Time:06-03

I got work like I need to call an API in the background every 28 minutes, So I thought of using setInterval to achieve this, but is it possible to set interval in minutes?

useEffect(() => {
setInterval(() => {
  //dispatch API here
},??)
})

Now here I need to run this every 28 minutes, How can I implement this?

If there are other possible ways to achieve my requirement also, please do recommend that way too.

Thanks in advance!!

CodePudding user response:

setInterval accepts the delay parameter in milliseconds. A millisecond is 1 1000th (1/1000) of a second.

So if you want a delay of 28 minutes, you need to work out how many seconds this is (28*60) and then multiply that by 1000.

So 28 * 60 * 1000 = 1,680,000

https://developer.mozilla.org/en-US/docs/Web/API/setInterval

Richard.

CodePudding user response:

Just use a variable. Like this...

useEffect(() => {
    let minutes= 28 * 60*1000;
    setInterval(() =>{
        console.log('hello world');
    },minutes);
})
  • Related