Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

document ready after dom manipulation

I'm doing an application with Phonegap and I'm using a self-built slide transition to change the pages. It works like this:

Every page is a div with 100% height and width, so if I change the Page, I set the next div right to the currently active and slide both to the left side.

Now to the Problem: the sliding works fine, but it's executed before the content of the right div is completely loaded. So the right div slides in empty, and only after a few hundred miliseconds the content will appear.

I tried it with document.ready, but as I've read this event is only executed the first time the DOM is loaded.

Does anybody know how I can wait for the DOM to be completely rendered again after I've manipulated the DOM with Javascript?

like image 770
Michael Kunst Avatar asked Oct 26 '12 08:10

Michael Kunst


People also ask

How do I know if my DOM is ready?

Use document. readyState === 'interactive' to detect when the DOM is ready.

Where do you put document ready?

So technically it doesn't matter where you put it. Many people like putting script in the head, because it makes sure the script is read before the page is loaded. Other people like putting it at the very end (just before the end body tag) so that all of the elements of the page are loaded before the script reads them.

How do I stop document ready function?

$(document). ready() is dependent on the onLoad event which is triggered by the browser meaning you can not prevent it from happening. If the alert() is determined by some condition then I would use an if/else statement to decide whether it is called.

What comes after document ready jQuery?

So, there is no event called after document. ready(). You'll need to create and wait for events to complete on your own, or use window. load().


1 Answers

In your case, you can pick one element in the content of the next div and keep checking it with $(...).length. If the value is > 0, the DOM is loaded and you can change the page.

You may want to try this function:

Function.prototype.deferUntil = function(condition, timeLimit) {
    var ret = condition();

    if (ret) {
        this(ret);
        return null;
    }

    var self = this, interval = null, time = ( + new Date());
    interval = setInterval(function() {
        ret = condition();
        if (!ret) {
            if (timeLimit && (new Date() - time) >= timeLimit) {
                // Nothing
            } else {
                return;
            }
        }
        interval && clearInterval(interval);
        self(ret);
    }, 20);

    return interval;
};

Usage:

(function() {
    console.log('Next page loaded');
}).deferUntil(function() {
    return $('#nextDiv').length > 0;
}, 3000);

The above example will check the div that has id="nextDiv" in every 20ms (not longer than 3 seconds). When the div is loaded, it will show 'Next page loaded' in the console.

You can try on this fiddle

like image 62
anvoz Avatar answered Sep 22 '22 16:09

anvoz