Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to execute a function after some time

Tags:

javascript

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.

like image 476
Rayan Sp Avatar asked Apr 09 '13 08:04

Rayan Sp


People also ask

How do you call a method after some time?

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.

How do you run a function after some time in react?

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);

How do you call a function after a delay?

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).


1 Answers

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.

like image 137
stef Avatar answered Oct 10 '22 13:10

stef