admin管理员组文章数量:1355559
I am trying to create a simple function when on click a series of style changes happen to an element.
I want to add a 4-second delay in between the two style changes that I make. I've found a few methods of adding delays before and after a function but not inside a function how can I achieve this?
My Code:
const btnEl = document.getElementById("btnel")
const subtl = document.getElementById("chp1sub")
document.getElementById("chp1sub").style.backgroundColor = "red";
btnEl.addEventListener("click", function(){
subtl.style.backgroundColor = "blue"
// 4 second delay here before running next line
subtl.style.transform = "translateY(-90px)"
})
Would really appreciate any advice on this
I am trying to create a simple function when on click a series of style changes happen to an element.
I want to add a 4-second delay in between the two style changes that I make. I've found a few methods of adding delays before and after a function but not inside a function how can I achieve this?
My Code:
const btnEl = document.getElementById("btnel")
const subtl = document.getElementById("chp1sub")
document.getElementById("chp1sub").style.backgroundColor = "red";
btnEl.addEventListener("click", function(){
subtl.style.backgroundColor = "blue"
// 4 second delay here before running next line
subtl.style.transform = "translateY(-90px)"
})
Would really appreciate any advice on this
Share Improve this question asked May 6, 2022 at 11:05 Xeon InkXeon Ink 431 gold badge1 silver badge7 bronze badges 2- Its called "timers": developer.mozilla/en-US/docs/Web/API/setTimeout – Marc Commented May 6, 2022 at 11:06
- 1 You could get the delay implemented via CSS animation. – A Haworth Commented May 6, 2022 at 11:19
3 Answers
Reset to default 4You can simply use the setTimeout
method, it runs a function after the time you set in milliseconds. To achieve what you need, you have to set an anonymous function to it.
More information
Example
setTimeout(() => {
/* Code to run after 4 seconds */
}, 4000)
With your code
const btnEl = document.getElementById("btnel")
const subtl = document.getElementById("chp1sub")
document.getElementById("chp1sub").style.backgroundColor = "red";
btnEl.addEventListener("click", function(){
subtl.style.backgroundColor = "blue"
setTimeout(() => {
// 4 second delay here before running next line
subtl.style.transform = "translateY(-90px)"
}, 4000)
})
You can simply use CSS transition-delay inside that function
Create an anonymous inner function called by setTimeout
const btnEl = document.getElementById("btnel")
const subtl = document.getElementById("chp1sub")
document.getElementById("chp1sub").style.backgroundColor = "red";
btnEl.addEventListener("click", function(){
subtl.style.backgroundColor = "blue"
setTimeout(function(){
subtl.style.transform = "translateY(-90px)"
},4000)
})
Alternately, because this is a CSS transition, you can use CSS transition-delay with this translation.
本文标签: htmlHow do I add delay within a function in JavascriptStack Overflow
版权声明:本文标题:html - How do I add delay within a function in Javascript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744052168a2582621.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论