KB.
KB.

Reputation: 3657

Closing bootstrap modal with setInterval causes repeated closures

I have a modal box (a bootstrap modal). After the user has made a particular ajax request and the 'okay' comes back from the server I have it set to close the dialog after 1.5 seconds.

setInterval(function(){ $('#modalname').modal('hide'); },1500);

This works okay the first time but thereafter each time I open the modal via a click of a button it keeps closing it automatically. If I remove the setInterval then it's fine.

Is there something I'm doing wrong here?

Upvotes: 0

Views: 1874

Answers (2)

Artyom Neustroev
Artyom Neustroev

Reputation: 8715

setInterval executes a function infinite amout of times every n milliseconds. You should use setTimeout:

setTimeout(function(){
   $('#modalname').modal('hide');
},1500);

Upvotes: 4

Ehsan Sajjad
Ehsan Sajjad

Reputation: 62488

Use setTimeout() instead, as you need to execute it onetime, setInterval() will call the function after every interval of specified time, while setTimeout() only calls once:

setTimeout(function(){ 

           $('#modalname').modal('hide'); 

          },1500);

Upvotes: 3

Related Questions