简体   繁体   English

更改JQuery就绪调用的顺序

[英]Change order of JQuery ready calls

I need to order some onReady calls, I was looking at these questions here and here and found nothing. 我需要订购一些onReady电话,我在这里这里看到这些问题,一无所获。 However it was quite long time ago, and maybe now there is something to workaround this. 然而,很久以前,也许现在有一些东西可以解决这个问题。 so I need to do something like this: 所以我需要做这样的事情:

$(function (){ console.log('second call'); })
$(function (){ console.log('first call'); })

To clarify why i need to do this: 澄清为什么我需要这样做:

The reason why I need that is these calls are spread through all the application, and what the most important: some of them are from third party and I cannot control them. 我需要的原因是这些调用是通过所有应用程序传播的,最重要的是:其中一些来自第三方,我无法控制它们。 But I do need to call my methods at first place, and i need to place scripts at the very and of the document. 但我确实需要在第一时间调用我的方法,我需要在文档和文档中放置脚本。 The code I working on is the legacy code and I cannot change it a lot, so answers like reconsider the way you are doing it would make no much sense for me. 我正在处理的代码是遗留代码,我无法对其进行大量更改,因此重新考虑您所采用的方式的答案对我来说没有多大意义。 I just wandering if it possible or any workaround. 如果可能或任何解决方法,我只是徘徊。

If you want to control the order of function execution, you should put them in same callback function. 如果要控制函数执行的顺序,则应将它们放在同一个回调函数中。

$(function() {
  console.log('second call');
  console.log('first call');
});

The principle is, the ready() function adds things to an array (readyList), and then executes them in the order added. 原理是,ready()函数将数据添加到数组(readyList),然后按添加的顺序执行它们。 Manipulating the readyList array may solve you requirement, as per this article here 操纵readyList阵列可以解决你的要求,按这篇文章在这里

You could use an array to store the functions and have one onReady call that invokes them in the right order. 您可以使用数组来存储函数,并使用一个onReady调用以正确的顺序调用它们。

var calls = [];

calls[2] = function() {
    console.log("third call");
};

calls[1] = function() {
    console.log("second call");
};

calls[0] = function() {
    console.log("first call");
};

$(function() {
    for (var index = 0; index < calls.length; index += 1) {
        calls[index]();
    }
});

I got around it by having a .ready() very early on (right after jQuery loads) that executes functions from an array. 我很早就开始使用.ready()(在jQuery加载之后)从数组中执行函数。 Because it's very early in the source code it's .ready() is executed first. 因为它在源代码中很早就会先执行.ready()。 Further down the page I can add functions to the array, so they all execute before any other .ready()s. 在页面的下方,我可以向数组添加函数,因此它们都会在任何其他.ready()之前执行。

I've wrapped it up in a .beforeReady() function and put it on GitHub as jQuery-Before-Ready for anyone that is interested. 我把它包装在一个.beforeReady()函数中,然后把它放在GitHub上作为jQuery-Before-Ready给任何感兴趣的人。

https://github.com/aim12340/jQuery-Before-Ready https://github.com/aim12340/jQuery-Before-Ready

usage: 用法:

$.beforeReady(function() {
    alert("Ere I am JH")
});

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

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