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

};

“选择器”是显示值的跨度选择器。“varAux”和“varAux2”是加号和减号按钮的选择器。

如果我为每个增量发送一个 Ajax 请求,客户端就会超载。我认为一个选项可能是知道一个按钮是否在确定的时间没有被点击。另一种方式?

我使用 jquery-ui 来加减按钮。

4

1 回答 1

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