简体   繁体   English

在继续使用document.ready代码之前等待第一个事件完成

[英]waiting for first event to finish before continuing with document.ready code

My question is simple. 我的问题很简单。

I have some configuration and loading that needs to be done before a document.ready() call. 我有一些配置和加载需要在document.ready()调用之前完成。

// initialize ASAP so it loads super fast
soundManager.setup({
    url: 'swf/',
    onready: SMOnReadyHandler
});

This onready event handler is likely to take, say, a long time before completion. 例如,这个onready事件处理程序可能需要很长时间才能完成。

After THIS, I want to call a document.ready() event listener to load the rest of my page, as so: 在这之后,我想调用document.ready()事件监听器来加载我的页面的其余部分,如下所示:

$(document).ready(function() { 
    // code here might be dependent on the completion of 
    // soundManager.onready event handler 
});

How do I ensure that $(document).ready occurs only after the soundManager.onready handler has completed fully? 如何确保$(document).ready仅在soundManager.onready处理程序完全完成后才会发生?

Thanks a lot in advance! 非常感谢提前!

You could use a deferred : 您可以使用deferred

var soundManagerDeferred = $.Deferred();

soundManager.setup({
    url: 'swf/',
    onready: SMOnReadyHandler
});

function SMOnReadyHandler() {
    soundManagerDeferred.resolve();
}

$(document).ready(function() {
    soundManagerDeferred.done(function() {
        //init
    });
});

You can't, because the browser is who determines when the DOM is ready. 你不能,因为浏览器是谁确定DOM准备好的时候。 My suggestion is, simply remove your document.ready handler, and pass your startup function to the sound manager's onready . 我的建议是,简单地删除您的document.ready处理程序,并通过您启动功能,声音管理器的onready I mean, replace this: 我的意思是,替换这个:

$(document).ready(function() { 
    // do init stuff 
});

With this: 有了这个:

function initStuff() {
    // do init stuff 
}
soundManager.setup({
    url: 'swf/',
    onready: initStuff
});

To ensure that you don't reference any element that's not in the DOM already, add the code to the end of your html, right before </body> . 为了确保您没有引用任何不在DOM中的元素,请在</body>之前将代码添加到html的末尾。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM