简体   繁体   English

如何使此更改边框颜色动画停止

[英]How to make this change border color animation stop

Hi I found this code and I'm wondering how to make it stop blinking after 3 seconds 嗨,我找到了此代码,我想知道如何使其在3秒后停止闪烁

function flashit()
{

var myborder = document.getElementById('my');

if (myborder.style.borderColor=="green") 

myborder.style.borderColor="red" ;

else 
myborder.style.borderColor="green" ;


} 
setInterval('flashit()', 400) ;

setTimeout and setInterval return an identifier that refers back to the timeout/interval; setTimeoutsetInterval返回一个标识符,该标识符返回到超时/间隔; which can then later be used to unset them. 然后可以用来取消设置它们。

So I changed your code to demonstrate. 因此,我更改了代码以进行演示。 Please comment if you have any questions. 如有任何疑问,请发表评论。

function flashit(){
    var myborder = document.getElementById('my');

    if (myborder.style.borderColor=="green"){
        myborder.style.borderColor="red" ;
    } else {
        myborder.style.borderColor="green" ;
    }
}
var intervalId = setInterval(flashit, 400) ;
setTimeout(function() {
    clearInterval(intervalId);
}, 3000);
var refreshIntervalId = setInterval(flashit, 400);

// Stop after 3 seconds
setTimeout(function(){clearInterval(refreshIntervalId)}, 3000);

JSFiddle 的jsfiddle

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM