我使用下面的递归函数,以便在httpstatus != 200
时重新打开网站:
retryOpen = function(){
this.thenOpen("http://www.mywebsite.com", function(response){
utils.dump(response.status);
var httpstatus = response.status;
if(httpstatus != 200){
this.echo("FAILED GET WEBSITE, RETRY");
this.then(retryOpen);
} else{
var thisnow = hello[variable];
this.evaluate(function(valueOptionSelect){
$('select#the_id').val(valueOptionSelect);
$('select#the_id').trigger('change');
},thisnow);
}
});
}
问题在于,retryOpen
函数有时甚至不会回调function(response){}
。然后,我的脚本冻结了。
我想知道如果没有来自网站的响应(甚至某些错误代码为404或其他东西),如何更改功能以便能够递归尝试再次打开网站?换句话说,如何重写retryOpen
函数,以便在函数在一定时间后没有达到回调时重新运行?
答案 0 :(得分:0)
我会尝试这样的事情。请注意这是未经测试的代码,但应该让您走上正确的路径
retryOpen = function(maxretry){
var count = 0;
function makeCall(url)
{
this.thenOpen(url, function(response){
utils.dump(response.status);
});
}
function openIt(){
makeCall.call(this,"http://www.mywebsite.com");
this.waitFor(function check() {
var res = this.status(false);
return res.currentHTTPStatus === 200;
}, function then() {
var thisnow = hello[variable];
this.evaluate(function(valueOptionSelect){
$('select#the_id').val(valueOptionSelect);
$('select#the_id').trigger('change');
},thisnow);
}, function timeout() { // step to execute if check has failed
if(count < maxretry)
{
openIt.call(this);
}
count++
},
1000 //wait 1 sec
);
}
openIt();
}