0

ボタンが決められた時間クリックされていないかどうかを知る方法はありますか? Ajaxリクエストで温度を制御するために、2つの増分ボタンと減分ボタン(プラスとマイナス)があります。値は、次の関数で 1 対 1 にインクリメントされます。

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);

};

「セレクター」は、値を表示するスパンのセレクターです。「varAux」と「varAux2」は、プラスとマイナスのボタンへのセレクターです。

インクリメントごとに Ajax リクエストを送信すると、クライアントが過負荷になります。ボタンが所定の時間クリックされていないかどうかを知ることができるオプションがあると思います。別の方法?

プラスボタンとマイナスボタンにはjquery-uiを使用しています。

4

1 に答える 1

1

AJAX リクエスト間に最小間隔を課すことができます。その間隔内でボタンが 2 回クリックされた場合、次のように 1 つの要求のみが実行されます。

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
}
于 2013-03-01T13:17:10.567 に答える