jQuery:在继续之前等待回调返回

发布于 2024-10-26 19:02:50 字数 293 浏览 2 评论 0原文

我有一个页面,当单击各种内容时,它使用相当多的 ajax 来加载其内容,现在我正在努力通过读取 url 中传入的哈希值来链接到页面的不同部分,作为一种小路。我想我可以浏览传入的哈希的不同部分,并使用 jQuery 的 .trigger() 触发右键单击。这样做的问题是,在该部分的内容实际加载之前(因为它是通过ajax加载的),下一秒触发器将被调用,这意味着它试图触发的东西不存在。在我使用下一个触发器之前,有没有一种好方法可以设置某种标志或某些东西来了解所有回调何时完成并且所有内容都已加载?或者我以完全错误的方式处理这个问题?只是寻找一些关于如何完成此任务的一般策略。

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. 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(). 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. 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

独木成林 2024-11-02 19:02:50

您想要创建一个成功回调。顾名思义,一旦加载成功完成,就会执行此代码。

$.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
  }
});

请参阅 http://api.jquery.com/jQuery.ajax/

You want to create a success callback. 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/

乖乖兔^ω^ 2024-11-02 19:02:50

如何使用

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

来等待请求加载

how about using the

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

to wait until request is loaded

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文