I want to write a javascript code where I can execute a function after exactly 30 minutes.
say I have a function called getScore
and another function called getResult
. I want those functions to be executed after exactly thirty minutes.
It's for a quiz purpose, the quiz duration is thirty minutes, so after the time passes, both functions should be executed.
You can use JavaScript Timing Events to call function after certain interval of time: This shows the alert box every 3 seconds: setInterval(function(){alert("Hello")},3000); You can use two method of time event in javascript.
The setTimeout method allows us to run a function once after the interval of the time. Here we have defined a function to log something in the browser console after 2 seconds. const timerId = setTimeout(() => { console. log('Will be called after 2 seconds'); }, 2000);
Use setTimeout() inbuilt function to run function after a delay in JavaScript. It's plain JavaScript, this will call your_func once, after fixed seconds (time).
You should use setTimeout()
:
setTimeout(function() {
getScore();
getResult();
}, 1800000);
The '1800000' is the time in milliseconds after which you want this function to execute. In this case, 30 minutes.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With