由于回调结构中可能存在错误,无法使超时工作

时间:2014-09-29 18:49:10

标签: javascript jquery

我正在尝试使用以下代码在单按按钮操作中调用服务器进程两次。我的要求声明我需要尝试两次,并更新我正在重试的用户。如果任何一个尝试都有效,那么我会提供一条成功消息,如果两个都失败了,那么我告诉他们再次尝试'。

按照编码,以下代码有效。唯一的问题是我希望每条消息都显示一秒钟,以便可以读取它们。

我已经尝试了setTimeout(1000)和delay(1000)的每个组合,它或者没有效果,或者在那个时候挂起。我怀疑我的回调结构存在逻辑缺陷。有人可以指出我做错了吗?

$(".infoMessage").text("Starting Open Process...");
var deferred = openATMRPC.openATMFirst();
deferred.addCallback(function(result){

    if (result == "false") {
      $(".infoMessage").text("Attempting Second try...");
      var insideDeferred = openATMRPC.openATMSecond();
       insideDeferred.addCallback(function(result) {
       if (result == "false") {
           $(".infoMessage").text("Try Again Later");
       } else {
            $(".infoMessage").text("Success 2");
        }
    })
  } else if(result == "true"){
      $(".infoMessage").text("Success 1");
    }
  }
)

注意:我的服务器进程返回一个" true"或"假"是否成功。

1 个答案:

答案 0 :(得分:1)

使用jQuery.Deferred()

尝试此模式
$(function () {
    $(".infoMessage").text("Starting Open Process...");
    // included `trueFalse` argument , here , to display flow of results
    // not certain if `openATMRPC.openATMFirst()` , 
    // `openATMRPC.openATMSecond()` returns a `promise` object ?
    // utilized same function applied to both `deferred` and `insideDeferred` 
    var deferred = function(trueFalse) {
        // return value from response as `deferred.promise`
        // within `setTimeout` , after `1000` ms (one second)
        return new $.Deferred(function (dfd) {
        setTimeout(function () {
            $.post("/echo/html/", {
                html: trueFalse
            })
        // `deferred.always` utilized here ,
        // to catch either `success` or `fail` ,
        // not certain about `error` (`fail`) callbacks ?
            .always(function (data) {
                dfd.resolve(data)
            })
        }, 1000);
        // return `deferred.promise()` , 
        // e.g., "true" , or "false"
        return dfd.promise()
    })
    };
    // `deferred()` function called with `"false"` argument
    deferred("false").done(function (result) {
        if (result == "false") {
            $(".infoMessage").text("Attempting Second try...");
            // `insideDeferred` = `deferred()` called with `"true"` argument 
            var insideDeferred = deferred("true");
            insideDeferred.done(function (result) {
                if (result == "false") {
                    $(".infoMessage").text("Try Again Later");
                } else {
                    $(".infoMessage").text("Success 2");
                }
            })
        } else if (result == "true") {
            $(".infoMessage").text("Success 1");
        }
    })
});

jsfiddle http://jsfiddle.net/guest271314/vpy207t9/

$(function () {
    $(".infoMessage").text("Starting Open Process...");
    var deferred = function(trueFalse) {
        return new $.Deferred(function (dfd) {
        setTimeout(function () {
                dfd.resolve(trueFalse)
        }, 1000);
        return dfd.promise()
    })
    };
    deferred("false").done(function (result) {
        if (result == "false") {
            $(".infoMessage").text("Attempting Second try...");
            var insideDeferred = deferred("true");
            insideDeferred.done(function (result) {
                if (result == "false") {
                    $(".infoMessage").text("Try Again Later");
                } else {
                    $(".infoMessage").text("Success 2");
                }
            })
        } else if (result == "true") {
            $(".infoMessage").text("Success 1");
        }
    })
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div class="infoMessage"></div>