Knowless
Knowless

Reputation: 49

Set interval change?

I have

<script type="text/javascript">
    $(document).ready(function(){

    setInterval(function(){
        $('#fetchstatuses').load('statuses/statuses.php')
    }, 1000);

});

<div id="fetchstatuses"></div>

I use this for loading statuses on page inside mentioned div above. But when I add comment form to statuses as:

for each status i echo status and on the bottom the code for comment with form with textarea and submit button....

My problem is that interval loads everything and i can't comment cuz textarea get reloaded in sec every time. When I remove interval from code, the whole script, my statuses got loading to long and images for like buttons load too long. Also when I try to stop the script till my comment form is opened and start after the comment it won't work. So does anyone know different way for this or something that can be used in this method??

Upvotes: 3

Views: 69

Answers (1)

philipp
philipp

Reputation: 16515

I do not fully understand the problem, but if it is about starting/stopping/pausing a timer/interval, than you have one Option for both setInterval() and setTimeout().

Both methods return the ID of the time-out or interval, which can be used to cancel each of them.

//1. getting the IDs
var timeOutID  = setTimeout(function () { … }, 1000),
    intervalID = setInterval(function () { … }, 1000);

//2. clearing the time-out/interval
clearTimeout(timeOutID);
clearInterval(intervalID);

Restarting is essentially step 1, but remind that the returned IDs are unique each time.

I hope I could help.

Upvotes: 1

Related Questions