admin管理员组文章数量:1399225
Below is some js that i have no idea how to modify to do what i want. Instead of onclick i need the js to activate after a delay of 40 seconds.
// retrieve the element
element = document.getElementById("ani");
// reset the transition by...
element.addEventListener("click", function(e){
e.preventDefault;
// -> removing the class
element.classList.remove("delay-1");
// -> triggering reflow /* The actual magic */
// without this it wouldn't work. Try unmenting the line and the transition won't be retriggered.
element.offsetWidth = element.offsetWidth;
// -> and re-adding the class
element.classList.add("delay-1");
}, false);
Below is some js that i have no idea how to modify to do what i want. Instead of onclick i need the js to activate after a delay of 40 seconds.
// retrieve the element
element = document.getElementById("ani");
// reset the transition by...
element.addEventListener("click", function(e){
e.preventDefault;
// -> removing the class
element.classList.remove("delay-1");
// -> triggering reflow /* The actual magic */
// without this it wouldn't work. Try unmenting the line and the transition won't be retriggered.
element.offsetWidth = element.offsetWidth;
// -> and re-adding the class
element.classList.add("delay-1");
}, false);
Share
Improve this question
asked Mar 26, 2014 at 13:12
user2810762user2810762
3956 silver badges12 bronze badges
1
- Just use a timeout. – gpgekko Commented Mar 26, 2014 at 13:15
3 Answers
Reset to default 8Use setTimeout to invoke a method/task after a specified number of milliseconds.
setTimeout(function() {
//your code/function here
}, (40*1000)); //40 seconds
Use the function setTimeout.
as seen in e.g. How do I delay a function call for 5 seconds?
In your case:
// retrieve the element
element = document.getElementById("ani");
// reset the transition by...
element.addEventListener("click", function(e){
e.preventDefault;
//introducing a delay of 40 seconds.
setTimeout(function() {
// -> removing the class
element.classList.remove("delay-1");
// -> triggering reflow /* The actual magic */
// without this it wouldn't work. Try unmenting the line and the transition won't be retriggered.
element.offsetWidth = element.offsetWidth;
// -> and re-adding the class
element.classList.add("delay-1");
}, 40 * 1000);
}, false);
use setTimeout function:
setTimeout(function() {
// retrieve the element
element = document.getElementById("ani");
// reset the transition by...
element.addEventListener("click", function(e){
e.preventDefault;
// -> removing the class
element.classList.remove("delay-1");
// -> triggering reflow /* The actual magic */
// without this it wouldn't work. Try unmenting the line and the transition won't be retriggered.
element.offsetWidth = element.offsetWidth;
// -> and re-adding the class
element.classList.add("delay-1");
}, false);
}, 40000);
本文标签: javascriptJS remove and add class timing functionStack Overflow
版权声明:本文标题:javascript - JS remove and add class timing function - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744118358a2591620.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论