2011-09-21 50 views
0

如何設置一個使用jQuery的函數,它允許我翻轉某個容器中的文本,例如「#rndbtn」,或者在.post表單的成功或者經過一段時間如6000ms。翻轉成功的文本或與Jquery延遲一段時間

我遇到了一個問題,請求時間過長,因此某些瀏覽器會在60秒後再次觸發該呼叫,所以我希望它只是翻轉文本以在該時間後提醒用戶失敗,而不是徵稅我的服務器一次又一次。如果

$.post("/sendreq", { arg1: arg1, arg2: arg2} , 

function(data,status,xhr) { 
    if (this.console && typeof console.log != "undefined"){ 
      console.log(status); 
      console.log(data); 
      console.log(xhr); 
    } 

    if (data == "Error") { 
     $("#rndbtn").text('Failed'); 
    } 
    else if (status == "error") { 
     if (this.console && typeof console.log != "undefined"){ 
        console.log(xhr.status + " " + xhr.statusText + " " +data); 
     } 
     $("#rndbtn").text('Failed'); 
             } 
    else { 
      if (type!=0) { 
        if (type!=4) { 
          $("#rndbtn").text('Sent Successfully'); 
        } 
        else { 
          $("#rndbtn").text('Added to List'); 
        } 
      } 
      else { 
        $("#rndbtn").text('Reset Successfully'); 
      } 
    } 

    $("#rndbtn").attr("href",""); 
}); 
} 

回答

0
$(function() {  
     //Calling after post 
     var isSuccess = false; 
     $.post('/ther/url/here', function(responseData) { 
      //will get called after the Ajax call was successfull 
      isSuccess = true; 
      $('#rndbtn').text('foo'); 
     }); 

     //This will fire 6 seconds after the dom had loaded. 

     setTimeout(function() { 
      if(isSuccess === false) { 
       $('#rndbtn').text('foo'); 
      } 
     }, 6000); 
    }); 
+0

的成功來自6000ms之前是有辦法增加一個,如果周圍$。員額請求,如果時間超過6000ms切換 – BillPull

+0

你可以使用一個標誌來標記的順利完成說發佈,上面添加。 – Paul