In your case, you can select one element in the contents of the next div and continue checking it with $(...).length . If the value is> 0, the DOM loads and you can change the page.
You can 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) {
Using:
(function() { console.log('Next page loaded'); }).deferUntil(function() { return $('#nextDiv').length > 0; }, 3000);
In the above example, a div with id="nextDiv" will be checked every 20 ms (no more than 3 seconds). When the div is loaded, the console displays "Next page loaded."
You can try fiddle
anvoz source share