gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 11:44:46 24 4
gpt4 key购买 nike

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

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

我已经尝试了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”或“false”字符串。

最佳答案

尝试这种模式,利用 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>

关于javascript - 由于回调结构中可能存在错误,无法实现超时工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26106733/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com