2015-01-08 13 views
1

假設我的ajax呼叫呼叫我的PHP函數來獲得一些結果。如果通話時間較長,如何在ajax上顯示消息「對不起,需要時間加載」

$('.ui-loader').show(); 
$.ajax({ 
     url: BASE_URL +'some_page.php', 
     type: 'POST', 
     datatype: 'json', 
     async:false, 
     success: function (response) { 

      // response will get my result from "somepage.php" 
      $('.ui-loader').hide(); 

     }, 
     error: function (jqXHR, textStatus, errorThrown) { 
      //alert(jqXHR.status); 
     } 
    }); 

我怎麼能顯示出另一種程序,該程序指出"Sorry it is taking time please wait"當Ajax響應需要時間的結果呢?

我遇到了ajaxStart()ajaxStop()但我該如何在這裏使用?你們能指導我實現這個目標嗎?

注:$('.ui-loader').show();是一個簡單的裝載機我想隱藏此加載器,並顯示彼此具有指出「對不起它需要時間請稍候」。

在此先感謝。

+0

http://stackoverflow.com/questions/68485/how-to-show-loading-spinner-in-jquery – Sadikhasan

回答

1

你可能用setTimeout像:

var longWaitTimerId = setTimeout(function() { 
    //took longer than 5 seconds 
}, 5000); 

$.ajax({ 
    ... 
    success: function (response) { 
     clearTimeout(longWaitTimerId); 
     ... 
    }, 
    error: ... same as above 
}); 
+0

'clearTimeout( longWaitTimerId);「請問能做什麼,請詳細解釋一下。當我需要顯示特定的加載器時,需要5秒以上的時間 –

+0

'setTimeout'基本上排隊一個函數在'x'毫秒內執行。 'setTimeout'返回定時器ID,以便它可以使用'clearTimeout'來取消。 – plalx

+0

好的,謝謝@plax生病嘗試這個解決方案讓你知道 –

0
function MakeAjaxCall(){ 
//timer to show another loader 
var timer = window.setTimeout(function(){ 
     $('.ui-loader').hide(); 
     $('.ui-AnotherLoader').show(); 
    }, 2000); 
$('.ui-loader').show(); 
$.ajax({ 
    url: BASE_URL +'some_page.php', 
    type: 'POST', 
    datatype: 'json', 
    async:false, 
    success: function (response) { 
      //clear the timer if ajax call take less time 
      wiundow.clreatTimeout(timer); 
     // response will get my result from "somepage.php" 
     $('.ui-loader').hide(); 
     $('.ui-AnotherLoader').hide(); 
    }, 
    error: function (jqXHR, textStatus, errorThrown) { 
     //alert(jqXHR.status); 
    } 
}); 
} 
+1

'wiundow.clreatTimeout(timer);'語法錯誤糾正它'window.clearTimeout(timer);' –

相關問題