简体   繁体   English

jQuery和JavaScript函数的顺序

[英]Order of jquery and javascript functions

I have a feeling I'm missing something obvious, but... 我觉得我缺少明显的东西,但是...

All my neat jQuery functions are being forced to wait by a particularly slow-moving javascript api call from within the body of the page. 我所有的整洁jQuery函数都被迫从页面正文中进行了特别缓慢的javascript API调用等待。 I'd like to have the jQuery run first, and then the api when that's done. 我想先运行jQuery,然后再运行api。 Is there a standard way of imposing the order? 有没有强制执行订单的标准方法?

easy workaround, call your api within a setTimeout statement. 简单的解决方法是,在setTimeout语句中调用您的api

Example: 例:

$(document).ready(function(){
     // beautiful jQuery code here
     setTimeout(function(){
        // terribly slow code here
     }, 100);
});

It is in general a good idea to use setTimeout on heavy code/DOM manipulation. 通常,在繁重的代码/ DOM操作上使用setTimeout是一个好主意。 It will avoid the browser from "freezing". 它将避免浏览器“冻结”。

Well if you just want the jquery before the javascript, do this: 好吧,如果您只想在JavaScript之前使用jquery,请执行以下操作:

function japi(){
   japi.dosomething();
   //Your api part here
}

$("#test").html("something");
//Lots of jquery here
japi();

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

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