使用突变处理器预防多个事件
我想根据进度(25%,50%,75%和100%)跟踪输入的动态值和触发事件。就我而言,输入是进度栏: progress bar
这是HTML代码,知道Aria-Valuetext是HTML代码动态:
<input tabindex="0" type="range" aria-hidden="false" aria-label="slide progress" aria-valuetext="30%">
为此,我使用突变处理器接口:
//Select the target node
const target = document.querySelector("[aria-valuetext]");
//create an observer instance
const observer = new MutationObserver(function(mutations, e){
let videoProgress = Math.round($("[aria-valuetext]").val()/1000);
if(videoProgression == 25){console.log('Progression:', videoProgression);
console.log('Video action: Progression 25%');}
if(videoProgression == 50){console.log('Progression:', videoProgression);
console.log('Video action: Progression 50%');}
if(videoProgression == 75){console.log('Progression:', videoProgression);
console.log('Video action: Progression 75%');}
if(videoProgression == 100){console.log('Progression:', videoProgression);
console.log('Video action: Progression 100%');}
});
const config = {attributes: true, childlist: true, characterData: true};
observer.observe(target, config);
我的问题是事件被多次触发。我试图使用预防违规和停止中间propagation,而没有任何结果。
I want to track a dynamic value of an input using jquery and trigger events depending on the progression (25%, 50%, 75% and 100%). In my case, the input is a progress bar:
Progress bar
Here is the html code, knowing that aria-valuetext is dynamic:
<input tabindex="0" type="range" aria-hidden="false" aria-label="slide progress" aria-valuetext="30%">
To do so, I use the MutationObserver interface:
//Select the target node
const target = document.querySelector("[aria-valuetext]");
//create an observer instance
const observer = new MutationObserver(function(mutations, e){
let videoProgress = Math.round($("[aria-valuetext]").val()/1000);
if(videoProgression == 25){console.log('Progression:', videoProgression);
console.log('Video action: Progression 25%');}
if(videoProgression == 50){console.log('Progression:', videoProgression);
console.log('Video action: Progression 50%');}
if(videoProgression == 75){console.log('Progression:', videoProgression);
console.log('Video action: Progression 75%');}
if(videoProgression == 100){console.log('Progression:', videoProgression);
console.log('Video action: Progression 100%');}
});
const config = {attributes: true, childlist: true, characterData: true};
observer.observe(target, config);
My issue is that the events are fired multiple times. I tried to use preventDefault and StopImmediatePropagation without any result.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您为什么不想使用
onChange
或oninput
事件跟踪输入值?MutationObserver
似乎有点过大,并打算在每个DOM突变上发射一个事件。您可以停止使用其方法,然后使用.observe()
方法再次开始观察。您可以对您的要求添加更多澄清吗?您要解决哪个问题?
Why don't you wanna track the input value using an
onchange
oroninput
event?MutationObserver
seems a bit overkill and is intended to fire an event on every DOM mutation. You can stop observing using its.disconnect()
method and start observing again using the.observe()
method.Can you add some more clarifications regarding your requirements? Which problem do you want to solve, exactly?