2011-09-21 71 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