使用标志!
最好使用一些布尔变量,并检查是否允许该操作而不是调用绑定和解除绑定(这是额外开销)。
如果您使用jQuery,则可以使用data()在该锚点上存储数据。
,如:
$('.testLink').on('click',function(){
//reference link
var link = $(this);
//check data
data = link.data('amIallowed');
//if no data existed yet
if(!data){
//set data and make it false to prevent further requests
link.data('amIallowed',{
allowRequest : false
});
//lets do ajax
doAjax();
}
else{ //if there was data
//and we were allowed
if(data.allowRequest){
doAjax(link);
}
}
});
//do ajax stuff here
function doAjax(link){
$.post('url',{param:val},function(){
//revert to true after request
link.data('amIallowed',{
allowRequest : true
});
});
}
,如果你不使用jQuery,你仍然可以使用简单的JS同样的壮举。 jQuery使用“expando-properties”将数据绑定到元素上。如果您可以使用expando-props,请使用变量。
谢谢!这清除了我的怀疑。 :) – Sushil 2012-02-16 09:50:25