简体   繁体   English

如何处理此文档准备就绪的情况?

[英]How do I handle this document ready situation?

Hopefully a very simple question. 希望这是一个非常简单的问题。 I have some various js functions written as such: 我有一些这样写的各种js函数:

var app={
    start:function(){
        //do some stuff and call calculate
    }, //end start
    calculate:function(){
        //do some more stuff
    } //end calculate
}; //end var app

var neato={
    go:function(){
        //do some stuff and call creation
    }, //end go
    creation:function(){
        //do some more stuff
    } //end creation
}; //end var neato

Which I could then start as follows: 然后我可以开始如下:

$(document).ready(app.start);
$(document).ready(neato.go);

Is there any way I can combine starting both functions in one document ready request?? 有什么办法可以在一个文档准备好请求中组合启动两个功能? I just can't seem to figure it out despite trying a few different possibilities. 尽管尝试了几种不同的可能性,但我似乎还是无法弄清楚。

Use an anonymous function and manually call both: 使用匿名函数并手动调用两者:

$(document).ready(function () {
    app.start();
    neato.go();
});

If you need to have the value of this be document in those functions, then use .call(this) instead of () . 如果您需要具有的价值thisdocument中的这些功能,然后使用.call(this)不是() And if you need to pass the arguments from the handler, use .call(this, arguments) . 如果需要从处理程序传递参数,请使用.call(this, arguments)

What you have already will work just fine... but to make it shorter: 您已经拥有的将可以正常工作...但是要使其更简短:

$(function(){
    app.start();
    neato.go();
});

or 要么

function startingFunctions(){
    app.start();
    neato.go();
}

$(startingFunction);

(note: $(function(){..}); is the same as document.ready) (注意: $(function(){..});与document.ready相同)

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

相关问题 在某些情况下$(document).ready中的元素未定义 - An element in $(document).ready is undefined in a certain situation 如何获得ready函数和另一个可以在document.ready中正常播放的函数? - How do I get a ready function and another function to play nicely in document.ready? 我应该把 $(document).ready() 放在哪里? - Where do I put the $(document).ready()? 我还需要$(document).ready(function(){})吗? - Do I still need $(document).ready(function(){ })? 我是否必须使用为 jQuery 准备的文件? - Do I have to use document ready for jQuery? 如何将所有单独加载的 jQuery 片段包装到 $(document).ready() 调用中? - How do I wrap all separately loaded jQuery snippets into a $(document).ready() call? 为什么jQuery(文档).ready工作不正常? /如何在jQuery中为元素添加CSS? - Why is jQuery(document).ready not working? / How do I add CSS to an element in jQuery? 如何在$(document).ready函数中添加多个函数 - How do I add more than one function in my $(document).ready function 如何调用jQuery函数,使其可以在document.ready函数中运行? - How do I call my jQuery function so that it will run in my document.ready function? 如何按需从JavaScript而不是(document).ready运行jQuery函数? - How do I run a jQuery function on demand from JavaScript and not on the (document).ready?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM