Execute the Setinterval Function Without Delay the First Time

Execute the setInterval function without delay the first time

It's simplest to just call the function yourself directly the first time:

foo();
setInterval(foo, delay);

However there are good reasons to avoid setInterval - in particular in some circumstances a whole load of setInterval events can arrive immediately after each other without any delay. Another reason is that if you want to stop the loop you have to explicitly call clearInterval which means you have to remember the handle returned from the original setInterval call.

So an alternative method is to have foo trigger itself for subsequent calls using setTimeout instead:

function foo() {
// do stuff
// ...

// and schedule a repeat
setTimeout(foo, delay);
}

// start the cycle
foo();

This guarantees that there is at least an interval of delay between calls. It also makes it easier to cancel the loop if required - you just don't call setTimeout when your loop termination condition is reached.

Better yet, you can wrap that all up in an immediately invoked function expression which creates the function, which then calls itself again as above, and automatically starts the loop:

(function foo() {
...
setTimeout(foo, delay);
})();

which defines the function and starts the cycle all in one go.

How to start setInterval loop immediately?

Keep it simple. You can use a named function instead of an anonymous function; call it and set an interval for it.

function doSomething() {
console.log("tick");
}
doSomething();
setInterval(doSomething, 9000);

Create a scope if necessary:

(function() {
function doSomething() {
console.log("tick");
}
doSomething();
setInterval(doSomething, 9000);
})();

Finally, the following works without creating or affecting x:

setInterval(function x() {
console.log("tick");
return x;
}(), 9000);

function in setInterval() executes without delay

http://jsfiddle.net/wWHux/3/

You called the function immediately instead of passing it to setInterval.

setInterval( change, 1500 ) - passes function change to setInterval

setInterval( change(), 1500 ) - calls the function change and passes the result (undefined) to setInterval

JavaScript: How to get setInterval() to start now?

Your method is THE normal way way of doing it.

If this comes up over and over, you could make a utility function that would execute the handler first and then set the interval:

function setIntervalAndExecute(fn, t) {
fn();
return(setInterval(fn, t));
}

Then, in your code, you could just do this:

var i = setIntervalAndExecute(dothis, 20000);

Nodejs setInterval and run first immediatly not after interval

For modern JavaScript await and async should be used instead of then and catch.

This will make many things easier, and the code becomes more readable. You e.g. can use a regular for loop to iterate over an array while executing asynchronous tasks within it. And use try-catch blocks in the same way as you would in synchronous code.

// a helperfunction that creates a Promise that resolves after
// x milliseconds
function wait(milliseconds) {
return new Promise(resolve => setTimeout(resolve, milliseconds))
}

async function rollDice(profilearray, browserarray, url) {
for (let i = 0; i < profilearray.length; i++) {
// depending how you want to handle the wait you would create
// the "wait"-Promise here
// let timer = wait(3000)

let testcafe = await createTestCafe("localhost", 1337, 1338, void 0, true);

try {
let runner = testcafe.createRunner();
inputStore.metaUrl = url;
inputStore.metaLogin = teamdataarray[0].email;
inputStore.metaPassword = teamdataarray[0].password;
inputStore.moderator = profilearray[i].profil;
inputStore.message = profilearray[i].template;
inputStore.channelid = profilearray[i].channelid;

let failedCount = await runner.src([__basedir + "/tests/temp.js"])
.browsers(browserarray)
.screenshots("", false)
.run()

if (failedCount > 0) {
// ...
} else {
// ...
}
} catch (err) {
console.log(profilearray[i].profil);
console.log("Testcafe Error" + error);
} finally {
testcafe.close();
}

// Here you would wait for the "wait"-Promise to resolve:
// await timer;
// This would have similar behavior to an interval.

// Or you wait here for a certain amount of time.
// The difference is whether you want that the time the
// runner requires to run counts to the waiting time or not.
await wait(3000)
}

return "Fertig"
}

How to implement setInterval() function with interval as Minutes

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.



Related Topics



Leave a reply



Submit