等待$。员额完成

编程入门 行业动态 更新时间:2024-10-06 14:29:01
本文介绍了等待$。员额完成的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

看来,我的脚本不希望等待$。员额调用来完成。这就是一个问题。下面是一些伪code:

It appears that my script does not want to wait for the $.post call to finish. Thats a problem. Here is some pseudo code:

<script type="text/javascript" language="javascript"> $(document).ready(function(){ // Global var, to be used everywhere in the ready scope // Note its default value! var test = false; $.post('test.php',{},function(result){ if(result=='yes') { // This gets executed! alert('Its true! Hurraaay!'); test = true; } else { test = false; } } if(test==false) { // THIS gets executed, despite the fact that we set test to true! alert('Awww....'); } // it reaches this, showing us that there was no error! alert('Im alive!!!'); // and a whoooole bunch of other code here... } </script>

什么是要确保我的帖子通话结束后继续之前,没有挂在浏览器的最佳方法是什么?希望的东西,是不是太乱了。 :)

What is the best way to make sure that my Post call is finished before continuing, without hanging the browser? Hoping for something that is not too messy. :)

推荐答案

不是太乱用回调正常。

刚刚创建。员额()通话之外的一些功能,并在。员额称之为()当你认为这是适当的。你让很多回调,并利用它们内部的AJAX在一个非常灵活的方式调用。

Just create some function outside the .post() call and call it inside .post() when you think it is appropriate. You make many callbacks and use them inside the AJAX calls in a really flexible way.

在你的情况,因为你只叫警报(),也没有必要建立额外的功能 - 只需拨打警报()在。员额()通话。如果你的code变大了,考虑创建独立的功能。

In your case, because you only call alert(), there is no need to create additional functions - just call alert() inside .post() call. If your code gets bigger, consider creating separate functions.

这是JavaScript和异步调用的工作方式。习惯了它,并利用其权力来写的很干净,维护code。

This is how JavaScript and asynchronous calls work. Get used to it and use its power to write very clean and maintainable code.

更多推荐

等待$。员额完成

本文发布于:2023-11-13 02:50:19,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1583224.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:员额

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!