prependTo() inside load() completion callback not trigger when using setTimeout

Viewed 53

Why prependTo() inside load() completion callback not trigger when using setTimeout()? It just only repeat load() result.php but not execute $('#loadmore').prependTo('#boxchat');. Basically, i used this following code:

function requestmessage() {
    $('#boxchat').load('result.php', function(){
        $('#loadmore').prependTo('#boxchat'); 
    });
    setTimeout(requestmessage, 1000);
};
requestmessage();

I want to make real time result.php update by using setTimeout() and prependTo() div(#loadmore) inside div(#boxchat) at the same time. Anyone does have solution?

2 Answers

The problem is with .load(). It will replace the content of $('#boxchat') with the content received from result.php on each completion. Instead of load, can you try using .ajax(), .get() or .post() (depending on your use case, .get() should work) and in the success / complete callback, give the call to prependTo()

Check your browser console. Maybe load() call encountered some exception (/error) that halted the execution of the script, so it did not even reach prependTo() call.

If you are using Google Chrome - that would be Ctrl + Alt + J.

Related