我有一个jQuery脚本,我正在调用一个启动构建的Jenkins Web服务器。这个构建需要花费很多时间才能完成,我无从知晓。在Jenkins中,您可以通过查看该构建的JSON API来了解构建是否已完成。在这里,您可以查找键值“building”,如果为false,则表示构建已完成。我在初次通话后12秒检查此物体。这12秒是一个我不想依赖的最佳猜测估计。
所以我考虑将我的查询放在while循环中并不断检查JSON对象,直到“building”计算结果为false。但这导致我的浏览器崩溃导致它太多了。
除了执行setTimeout之外还有其他方法吗?
jQuery.ajax({
url:
"jenkins url that calls build",
type: "GET",
beforeSend: function () {
},
complete: function () {
},
success: function () {
/** Give build 12 secs to finish. **/
setTimeout(function() {
jQuery.ajax({
url:
"json url that stores the results of build",
type: "GET",
beforeSend: function () {
},
complete: function () {
},
success: function (lastBuild) {
}
});
}, 12000);
}
});
使用while循环
jQuery.ajax({
url:
"jenkins url that calls build",
type: "GET",
beforeSend: function () {
},
complete: function () {
},
success: function () {
var building = false;
// Freaks out here.
while (!building) {
jQuery.ajax({
url:
"json url that stores the results of build",
type: "GET",
beforeSend: function () {
},
complete: function () {
},
success: function (lastBuild) {
building = lastBuild["building"];
}
});
}
});
答案 0 :(得分:1)
您可以使用递归函数,并在成功函数中调用它。
var building=false;
function callFunction()
{ jQuery.ajax({
url:
"json url that stores the results of build",
type: "GET",
beforeSend: function () {
},
complete: function () {
},
success: function (lastBuild) {
building=lastBuild["building"];
if(building!=true) callFunction();
}
});
}