做 while javascript 问题 [英] Do while javascript issue

查看:18
本文介绍了做 while javascript 问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在 do while 循环中发送多个帖子,但未添加结果

I'm trying to send multiple post within a do while loop but the result is not added

<script type="text/javascript">
function action() {
    var initval = 1;
    var endval = 5;
    do {
    var action_string = 'txtuser=someone';

    $.ajax({
                    type: "POST",
                    url: "http://localhost/js.php",
                    data: action_string,
                    success: function(result){
                       $('div#append_result').append(initval + ',<br/>');
                     }  
                });
    initval++;
     } while (initval <= endval);
  }
</script>

输出是:5、5、5、5、5、

The Output is: 5, 5, 5, 5, 5,

我需要输出为:1、2、3、4、5、

and I need the output to be: 1, 2, 3, 4, 5,

推荐答案

由于 AJAX 的异步性质,当您的成功函数为任何结果 AJAX 请求运行时,循环已经完成并且 initval 设置为 5.您需要在每个请求开始时捕获 initval 的状态,并在 success() 方法中使用捕获的状态.关闭值是最简单的方法:

Due to the async nature of AJAX, by the time your success function runs for any of the resulting AJAX requests, the loop has completed and initval is set to 5. You need to capture the state of initval at the start of each request and use that captured state in the success() method. Closing over the value is the simplest way to do it:

function action() {
    var initval = 1;
    var endval = 5;
    do {
        var action_string = 'txtuser=someone';

        ( function( captured_initval ){
            $.ajax({
                type: "POST",
                url: "http://localhost/js.php",
                data: action_string,
                success: function(result){
                    $('div#append_result').append(captured_initval + ',<br/>');
                }  
            });
        }( initval ) );

        initval++;
    } while (initval <= endval);
}

但是要明白,一个或多个请求可能会挂在服务器上,允许后一个请求先完成,这可能导致 1,2,5,3,4 或类似的

Understand, though, that one or more requests could get hung up at the server allowing a latter request to complete first, which could result in 1, 2, 5, 3, 4 or something like that.

此外,使用元素的 ID 比使用元素标签名称作为哈希选择器的前缀要快得多.另外,每次成功运行时,您应该避免为结果 DIV 重新查询 DOM.抓住它一次并在需要时使用它:

Also, using an element's ID is much faster than prefixing the hash selector with the elements tag name. Plus you should avoid re-querying the DOM for your result DIV every time the success runs. Grab it once and use it when needed:

function action() {
    var initval = 1;
    var endval = 5;
    do {
        var action_string = 'txtuser=someone',
            $AppendResult = $('#append_result');

        ( function( captured_initval ){
            $.ajax({
                type: "POST",
                url: "http://localhost/js.php",
                data: action_string,
                success: function(result){
                    $AppendResult.append(captured_initval + ',<br/>');
                }  
            });
        }( initval ) );

        initval++;
    } while (initval <= endval);
}

这篇关于做 while javascript 问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆