admin管理员组文章数量:1401849
I'm trying to fire an event listener callback just once. addEventListener
conveniently has a flag that I can pass into it:
once: A Boolean indicating that the listener should be invoked at most once after being added. If it is true, the listener would be removed automatically when it is invoked.
This works fantastic in every browser except IE11 and Edge. I know that I can manually remove the event listener after it fires, but this flag should work... Am I doing something wrong, or is this an acknowledged issue with IE or what?
Should you prefer JSFiddle, otherwise:
var div = document.getElementById('transition');
var result = document.getElementById('result');
window.grow = function(){
var width = div.offsetWidth + 50;
div.style.width = width + 'px';
}
var count = 0;
div.addEventListener('transitionend', function(){
result.innerHTML = "transitionend has fired " + ++count + " times";
}, { once: true });
#transition{
background-color: yellow;
transition: width 2s;
width: 100px;
}
<div id="transition">
Content
</div>
<button onclick="grow()">
Grow the div
</button>
<div id="result">
</div>
I'm trying to fire an event listener callback just once. addEventListener
conveniently has a flag that I can pass into it:
once: A Boolean indicating that the listener should be invoked at most once after being added. If it is true, the listener would be removed automatically when it is invoked.
This works fantastic in every browser except IE11 and Edge. I know that I can manually remove the event listener after it fires, but this flag should work... Am I doing something wrong, or is this an acknowledged issue with IE or what?
Should you prefer JSFiddle, otherwise:
var div = document.getElementById('transition');
var result = document.getElementById('result');
window.grow = function(){
var width = div.offsetWidth + 50;
div.style.width = width + 'px';
}
var count = 0;
div.addEventListener('transitionend', function(){
result.innerHTML = "transitionend has fired " + ++count + " times";
}, { once: true });
#transition{
background-color: yellow;
transition: width 2s;
width: 100px;
}
<div id="transition">
Content
</div>
<button onclick="grow()">
Grow the div
</button>
<div id="result">
</div>
Share
Improve this question
edited Jul 14, 2021 at 17:38
Brian Tompsett - 汤莱恩
5,89372 gold badges61 silver badges133 bronze badges
asked Dec 30, 2016 at 21:38
adamdportadamdport
12.7k14 gold badges73 silver badges92 bronze badges
1
- 4 it's a new trick, which old dogs can't do. – dandavis Commented Dec 30, 2016 at 21:49
1 Answer
Reset to default 5Check the browser patibility section of the docs: no IE/edge support. caniuse is also helpful.
本文标签: javascriptoncetrue with addEventListener doesn39t work on IE11 or EdgeStack Overflow
版权声明:本文标题:javascript - once:true with addEventListener doesn't work on IE11 or Edge - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744264511a2597883.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论