2013-03-01 103 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

相关问题