如何绑定到模糊和变化,但只能在Jquery / Javascript中触发一次函数? [英] how to bind to both blur and change, but only trigger a function once in Jquery/Javascript?
问题描述
由于Ipad在('更改')上遇到麻烦,所以我也想绑定到'模糊',这在Ipad上工作正常。
然而,我不希望这两个事件触发这个功能两次,所以我需要一些钩子,以确保改变和模糊触发器,底层函数只会触发一次。
这是我现在在做的,但...不是很好
//确保绑定只能分配一次
var compSel = $('#my_select');
if(compSel.jqmData('bound')!= true){
console.log(bound);
compSel.jqmData('bound',true)
.on($ .support.touch?'blur':'change',function(){
console.log(trigger );
//运行函数xyz
})
}
如果您可以使用所有可触摸设备,使用模糊进行操作,则此功能将起作用。有没有人有更好的想法来确保模糊和更改只触发一个功能?
/ p> 感谢您的帮助! 尝试创建一个绑定到两个事件的函数,并添加一个超时来调用该函数。这样,如果函数被多次调用,它将只运行一次。 这样的事情(如果需要,可以调整超时): I'm trying to trigger a function when a select element is changed. Since Ipad is having trouble with on('change'), I also want to bind to 'blur', which works fine on Ipad. However I don't want both events to trigger the function twice, so I need some kind of hook to make sure if both change and blur trigger, that the underlying function is only fired once. This is what I'm doing now, but ... not very nice: This works if you can live with all touchable devices making do with blur. Question: Thanks for help! Try creating a function, bound to both events, and adding a timeout to call the function. This way, if the function is called multiple times, it will only run once. Something like this (you can adjust the timeout if needed):
这篇关于如何绑定到模糊和变化,但只能在Jquery / Javascript中触发一次函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
function blurChange(e){
clearTimeout(blurChange.timeout);
blurChange.timeout = setTimeout(function(){
//你的事件代码到这里
},100);
}
$('#my_select')。on('blur change',blurChange);
// make sure binding is only assigned once
var compSel = $('#my_select');
if ( compSel.jqmData('bound') != true ){
console.log("bound");
compSel.jqmData('bound', true)
.on( $.support.touch ? 'blur' : 'change', function(){
console.log("trigger");
// run function xyz
})
}
Does anyone have a better idea to make sure blur and change only trigger a function once?function blurChange(e){
clearTimeout(blurChange.timeout);
blurChange.timeout = setTimeout(function(){
// Your event code goes here.
}, 100);
}
$('#my_select').on('blur change',blurChange);