How to use clearInterval() and then make changes to the DOM?

Posted by George D. on Stack Overflow See other posts from Stack Overflow or by George D.
Published on 2012-10-27T16:53:13Z Indexed on 2012/10/27 17:00 UTC
Read the original article Hit count: 206

Filed under:
|

I have this code and the problem I have is that I want to stop the loop and then replace the text 'Done!' that comes from the sms-loader.php script with the "textnew" string. Problem is that the loop occurs one more time so the text in the div.checkstatus field is again replaced by the calling php script.

The strange thing is that I see the log message and again I get a new (and final) request, although the ordering is the opposite (first stop then replace text()) in my script. I need to understand why this is happening.

$(document).ready(function() {
var interval = "";

$('.checkstatus').each(function(){
    var msgid = $(this).data('msg')
        $this = $(this),
        hid = $this.data('history'),
        textnew = '<a href="sms-dstatus.php?id='+msgid+'&sid=' +hid+ '&amp;keepThis=true&amp;TB_iframe=true&amp;height=430&amp;width=770" title="Delivery Status" class="thickbox"><img src="../template/icons/supermini/chart_curve.png" alt="status" width="16" height="16" /></a>';

    interval = setInterval(function() {
    $this.load('../pages/sms-loader.php?id='+msgid);

        // stop the loop
        if($this.text()=='Done!'){
            // stop it
            clearInterval(interval);
            console.log(textnew);
            this.html(textnew); /// this line is the problem
        }

    }, 5000); // 5 secs

});
});

© Stack Overflow or respective owner

Related posts about jQuery

Related posts about dom