首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >有没有办法知道某个按钮是否在确定的时间内没有被点击?

问有没有办法知道某个按钮是否在确定的时间内没有被点击?
EN

Stack Overflow用户
提问于 2013-03-01 21:03:55
回答 1查看 104关注 0票数 0

有没有办法知道某个按钮是否在确定的时间内没有被点击?我有两个递增和递减按钮(加号和减号),用于通过Ajax请求控制温度。使用下一个函数,该值将以一比一递增:

代码语言:javascript
复制
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来添加和减去按钮。

EN

回答 1

Stack Overflow用户

发布于 2013-03-01 21:17:11

您可以设置AJAX请求之间的最小间隔。如果在该时间间隔内单击该按钮两次,则只会执行一个请求,如下所示:

代码语言:javascript
复制
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
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/15158476

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档