想了解domReady.done,domReady.timer是什么意思?

时间:2012-09-27 08:46:20

标签: jquery javascript javascript-framework unobtrusive-javascript

我在JavaScript书中找到了这个例子

// Checks to see if the DOM is ready for navigation
function isDOMReady() {
    // If we already figured out that the page is ready, ignore

    if (domReady.done) return false;
    // Check to see if a number of functions and elements are
    // able to be accessed
    if (document && document.getElementsByTagName && document.getElementById && document.body) {
        // If they're ready, we can stop checking
        clearInterval(domReady.timer);
        domReady.timer = null;
        // Execute all the functions that were waiting
        for (var i = 0; i < domReady.ready.length; i++)
        domReady.ready[i]();
        // Remember that we're now done
        domReady.ready = null;
        domReady.done = true;
    }
}

// calling the domReady function
domReady(function () {
    alert("The DOM is loaded!");
    tag("h1")[0].style.border = "4px solid black";
});

想了解domReady.donedomReady.timer的含义是什么?

1 个答案:

答案 0 :(得分:3)

domReady.done是一个标志,一旦DOM准备好就会设置为true。 domReady.timer是以window.setInterval开头的间隔的引用/句柄,因此只要DOM准备就可以用window.clearInterval()清除它,因为不再需要检查。< / p>