gpt4 book ai didi

javascript - 如何让这个 Jquery 闪烁的背景在几秒钟后停止?

转载 作者:行者123 更新时间:2023-11-30 17:06:58 25 4
gpt4 key购买 nike

如何让这个 Jquery 闪烁的背景在几秒钟后停止?我正在尝试将背景和文字设置为闪烁,然后在 3 秒后停止。提前感谢您的帮助!

$(document).ready(function() {
blinkFont();
});

function blinkFont() {
document.getElementById("blink").style.color = "red"
document.getElementById("blink").style.background = "black"
setTimeout("setblinkFont()", 500)
}

function setblinkFont() {
document.getElementById("blink").style.color = "black"
document.getElementById("blink").style.background = "red"
setTimeout("blinkFont()", 500)
}
#blink {
text-align: center;
background: #000000;
color: #F00;
margin: 0;
padding: 20px;
}
#blink span {
font-size: 2em;
font-weight: bold;
display: block;
}
<div id="blink"><span>This is blinking text and background</span>
</div>

最佳答案

虽然我可以想出一些更优雅的方法来做到这一点,但无需太多改变您当前的结构,您可以将超时存储在变量中,然后使用 clearInterval ,它会停止计时器,以在三秒后停止重复:

<script>
var intervalA;
var intervalB;

$(document).ready(function() {

blinkFont();
setTimeout(function() {

clearInterval(intervalA); clearInterval(intervalB);},3000);
});

function blinkFont() {
document.getElementById("blink").style.color = "red"
document.getElementById("blink").style.background = "black"
intervalA = setTimeout("setblinkFont()", 500);
}

function setblinkFont() {
document.getElementById("blink").style.color = "black"
document.getElementById("blink").style.background = "red"
intervalB = setTimeout("blinkFont()", 500);
}

</script>
</head>

<body>
<div id="blink"><span>This is blinking text and background</span>
</div>
</body>

关于javascript - 如何让这个 Jquery 闪烁的背景在几秒钟后停止?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27812928/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com