Rayan Sp
Rayan Sp

Reputation: 1020

How to execute a function after some time

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.

Upvotes: 19

Views: 38931

Answers (2)

Agiel
Agiel

Reputation: 46

If You Want To Show Element ,you can use This Code , This Will Show Button.

     function myFunc(){
        var linkEd = document.getElementById("buttonID");
        var timing = 6;
        var text_timing = document.createElement("span");
        linkEd.parentNode.replaceChild(text_timing, linkEd);
        var id;
        id = setInterval(function() {timing--;
      if (timing < 0) {
        clearInterval(id);
       text_timing.parentNode.replaceChild(linkEd, text_timing);
      } else {
       text_timing.innerHTML = "<h3 class='yourclass'>Button will appear after  <strong>" + timing.toString() + "</strong>  second</h3>";
      linkEd.style.display = "inline";
      }
    }, 1000);
}

This Is To Showing Time miliseconds

<h3 class='yourclass'>Button will appear after <strong>" + timing.toString() + " </strong> second</h3>


To Call Time And show Button Use onclick event to show button after time 0

<button class='yourclass' onclick='myFunc()' id='button'>show time</button>

use display none to hide the button

<button style='display:none' id='buttonID' class='yourclass'>after time</button>

Upvotes: -2

stef
stef

Reputation: 14268

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.

Upvotes: 41

Related Questions