IgorAlves
IgorAlves

Reputation: 5550

How to create a function setInterval with clearInterval inside it?

I have a app that must send the user to homepage after some events. For this I use this bit of code that works good:

var waitime = 1000;
var handle=setInterval(function () {  
        $('.wrapper').html(divResp);
        $('body').append(js);
        clearInterval(handle);   
    }, waitime);

But I was trying to create a function to be called instead copy the code every time. So, after some reseach setInterval and how to use clearInterval and clearInterval outside of method containing setInterval I have create this one:

function refreshToHomePage3(handle,waitime){
     return setInterval(function () {  
                    $('.wrapper').html(divResp);
                    $('body').append(js);
                    clearInterval(handle);   
                  }, waitime);
}    

The problem is when a call the function, like this:

var refreshIntervalId=refreshToHomePage3(refreshIntervalId,waitime);

I have a infinite loop. I already solved the problem using setTimeout instead of setInterval and the function became like this one:

function refreshToHomePage2(waitime){
     setTimeout(function () {  
           $('.wrapper').html(divResposta);
           $('body').append(js); 
     }, waitime);
} 

But I was wondering how to solve the problem using setInterval and clearInterval. Any thougths?

Upvotes: 0

Views: 1167

Answers (3)

Sampath Liyanage
Sampath Liyanage

Reputation: 4896

setTimeout is prefered here. But you can use setInterval like this..

function refreshToHomePage3(handle,waitime){
     handle = setInterval(function () {  
                    $('.wrapper').html(divResp);
                    $('body').append(js);
                    clearInterval(handle);   
                  }, waitime);
     return handle;
}   

Actually there is no need to pass a handle variable into the function.

function refreshToHomePage3(waitime){
     var handle = setInterval(function () {  
                    alert("called after waitime");
                    clearInterval(handle);   
                  }, waitime);
     return handle;
}   

var handle = refreshToHomePage3(5000);

Upvotes: 1

Timo D
Timo D

Reputation: 1803

If you want your code to be executed just once after the wait time, setInterval is not the right function for this job, but setTimeout is.

setInterval will execute your code every n seconds until you execute clearInterval. However, setTimeout will execute your code once after n seconds and is therefore the correct approach for your problem.

Don't try to make setInterval something that it isn't :)

Upvotes: 0

Amit Joki
Amit Joki

Reputation: 59232

You're clearing the interval after the first time the code runs. So what you're doing is just what setTimeout does. You need setTimeout which runs only once after the waiting for waitTime.

function refreshToHomePage(handle, waitime) {
    setTimeout(function() {
        $('.wrapper').html(divResp);
        $('body').append(js);
        clearInterval(handle);
    }, waitime);
}

Upvotes: 0

Related Questions