Muntadar Muhammad
Muntadar Muhammad

Reputation: 115

javascript: how can I stop a function after specific time from calling it

simply I want to stop a function after specific time like 5 seconds from calling it.

I couldn't find a way to do it, can somebody help me

function popUp() {
    // Do some Thing 
    });
popUp();
// how to stop popUp() after calling it after 5 seconds from calling it??

Upvotes: 0

Views: 8190

Answers (3)

cmac
cmac

Reputation: 3268

How about you call your function, and then call setTimeout inside your function after 5 seconds to change display to "none", like this:

function popUp() {
  // Do some Thing

  // SetTimeout to change display to none
  setTimeout(function () {
    document.getElementById('div-id').style.display = "none";
  }, 5000);   
});
popUp();

Upvotes: 0

Just use return statement:

function popUp() {
    // Do some Thing 
    //Timer simulator
for (i = 0; i < 100; i++) { 
    if (i == 99) return
           }
    });

Upvotes: 1

Noah
Noah

Reputation: 570

You can use setTimout to run a function after a set amount of time. For example:

setTimeout(hidePopup, 5000); 

Will run the below function after 5 seconds (5000 milliseconds):

function hidePopup() {
  // Do the opposite
}

Upvotes: 1

Related Questions