揮発性のメモ2

http://d.hatena.ne.jp/iww/

$.ajax()で成功するまでリトライさせる

何秒間かリトライをがんばる
ファイルを作る人がいて、その人がファイルを作るまでがんばる

limittime = (new Date).getTime() + 4000; // 現在時刻 +4秒までリトライ頑張る

$.ajax({
    url: "hoge.json",
    dataType: 'json',
    error:function(xhr) {
        var nowtime = (new Date).getTime();
        if( limittime<nowtime ) {
            console.log("タイムアウト");
            console.log(xhr.status + " " + xhr.statusText);
        }else{
            console.log("リトライ");
            var x = this;
            setTimeout( function(){$.ajax(x)}, 500 ); // 0.5秒後にリトライ
        }
    },
    timeout: 4000,
    success: hogehoge
  });