Is it possible to change the time for the setInterval schedule conditions?

Asked 1 years ago, Updated 1 years ago, 101 views

Hello, I'm unemployed and preparing to get a job as a publisher.

I made a code that repeats with setInterval.

I want to set it to 4 seconds for the first time and 5 seconds for the second time.

By any chance, can I change the time of setInterval if the conditions are met?

I made the sauce below according to the conditions above.

setTimeout(function(){
    motionTimer();
    setInterval(motionTimer,4000)
},3500)

If you make it like this, it does work.

But I'm asking if this is how you make it like this.

timer javascript

2022-09-21 19:07

1 Answers

Yes, you made it well.

If you want to change the time in the middle of the setInterval, you can also use setTimeout as follows.

var count = 1;

function myTimeoutFunction()
{
    motionTimer();

    // There may be other conditions here.
    var timeout = count == 1 ? 3500 : 4000; 
    setTimeout(myTimeoutFunction, timeout);
    count += 1;
}

myTimeoutFunction();


2022-09-21 19:07

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.