2013-03-01 111 views
0

有沒有什麼辦法可以知道某個按鈕是否在確定的時間點擊? 我有兩個遞增和遞減按鈕(加號和減號)來控制Ajax請求的溫度。該值與下一個函數一對一地遞增:有沒有什麼辦法可以知道一個按鈕沒有按下確定的時間點擊?

Plugin_increment.prototype.startPluginTempe = function (selector, dataAux, dataAux2, varAux, varAux2) { 
    var valueElement = $(selector); 
    function incrementValue(e){ 
     if((valueElement.text() < 40) && (valueElement.text() > 10)){ //max and min 
      valueElement.text(Math.max(parseInt(valueElement.text()) + e.data.increment)); 
     } 

     if(valueElement.text() == 40){//max 
      if(e.data.increment == -1){ 
       valueElement.text(Math.max(parseInt(valueElement.text()) + e.data.increment)); 
      } 
     } 
     if(valueElement.text() == 10){//min 
      if(e.data.increment == 1){ 
        valueElement.text(Math.max(parseInt(valueElement.text()) + e.data.increment));     
       } 
     } 
     //Ajax request?????? 
     return false; 
    } 
    $(varAux).button({ 
       icons: { 
       primary: "ui-icon-plusthick" 
       }, 
       text: false 
     }).bind('click', {increment: 1}, incrementValue);  
    $(varAux2).button({ 
       icons: { 
       primary: "ui-icon-minusthick" 
       }, 
       text: false 
     }).bind('click', {increment: -1}, incrementValue); 

}; 

「selector」是顯示值的跨度選擇器。 「varAux」和「varAux2」是加號和減號按鈕的選擇器。

如果我爲每個增量發送一個Ajax請求,客戶端將被重載。我認爲,一個選項可能是知道按鈕是否沒有被點擊確定的時間。其他方式?

我使用jquery-ui加號和減號按鈕。

+1

爲什麼在ajax請求完成之前不關閉按鈕? – 2013-03-01 13:11:44

+0

因爲增量是一對一的,所以如果我想增加五個統一體的價值。將有五個Ajax請求。將會有不必要的請求 – vicenrele 2013-03-01 16:37:32

回答

1

您可以在AJAX請求之間施加最小時間間隔。如果在該時間間隔內點擊兩次按鈕,將只執行一次請求,如下所示:

function incrementValue(e) { 
    //your existing code here 
    scheduleAjaxRequest(); 
} 

var minimumTimeBetweenAjaxRequests = 500; // in milliseconds 
var ajaxRequestIsScheduled; 

function scheduleAjaxRequest() { 
    if (ajaxRequestIsScheduled) { 
     // two or more clicks within specified interval, 
     // the data will be sent in request that's already sceheduled 
     return; 
    } 
    ajaxRequestIsScheduled = setTimeout(doAjaxRequest, minimumTimeBetweenAjaxRequests); 
} 

function doAjaxRequest() { 
    //Ajax request 
    ajaxRequestIsScheduled = null; // clear timeout ID to allow next request to be scheduled 
} 
+2

這種方法的一個潛在問題是,如果由於某種原因AJAX請求失敗,客戶端數據和服務器端數據將不同步。所以你應該考慮適當地處理AJAX錯誤 – 2013-03-01 13:25:29

+0

是一個好主意,但是我寧願在多次點擊停止時只實現一個請求。最終目的是減少Ajax請求,而不是延遲它們。會有不必要的要求 – vicenrele 2013-03-01 16:35:50

相關問題