有没有办法知道某个按钮是否在确定的时间内没有被点击?我有两个递增和递减按钮(加号和减号),用于通过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来添加和减去按钮。
发布于 2013-03-01 21:17:11
您可以设置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
}https://stackoverflow.com/questions/15158476
复制相似问题