簡體   English   中英

網站未回應時重試

[英]Retry when no response from website

我使用下面的遞歸函數,以便在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函數,使其在經過一定時間后仍未到達回調時重新運行?

我會嘗試這樣的事情。 請注意,這是未經測試的代碼,但應該可以幫助您找到正確的路徑

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();
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM