简体   繁体   English

jQuery:等待回调返回,然后继续

[英]jQuery: Waiting for callbacks to return before continuing

I have a page which uses a fair bit of ajax to load its contents when various things are clicked and right now I am working on being able to link to the different parts of the page by reading the incoming hashes in the url as a sort of path. 我有一个页面,当单击各种东西时,它使用相当多的ajax来加载其内容,现在,我正在努力通过读取url中传入的哈希值来链接到页面的不同部分,路径。 I figured that I could just go through the different sections of the hash that comes in and trigger the right clicks using jQuery's .trigger(). 我认为我可以遍历哈希的不同部分,并使用jQuery的.trigger()触发右键单击。 The problem in doing this is that the next second trigger will be called before the contents of the section is actually loaded (as it is being loaded via ajax) meaning the thing it is trying to trigger doesn't exist. 这样做的问题是,在实际加载该节的内容之前(因为它是通过ajax加载的),将调用下一个第二个触发器,这意味着它要触发的事物不存在。 Is there a good way to set sort of a flag or something to know when all the callbacks complete and all of the content is loaded before I use the next trigger? 在我使用下一个触发器之前,是否有一种很好的方法来设置标记的种类或要知道何时完成所有回调并加载所有内容的某种方式? Or am I approaching this the completely wrong way? 还是我以完全错误的方式来处理这个问题? Just looking for some general strategy on how to get this done. 只是寻找一些有关如何完成此工作的一般策略。

You want to create a success callback. 您要创建一个success回调。 As the name implies, this code will be executed once your load has completed successfully. 顾名思义,此代码将在成功完成加载后执行。

$.ajax({
  url: 'ajax/test.php',
  success: function(data) {
    // your code to execute after load is complete
    // data parameter contains any information returned from test.php
  }
});

See "Callback Function Queues" on http://api.jquery.com/jQuery.ajax/ 请参阅http://api.jquery.com/jQuery.ajax/上的“回调函数队列”

how about using the 如何使用

$.ajax({
  url: 'ajax/test.php',
  async:false
});

to wait until request is loaded 等待直到请求被加载

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

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