[英]how to delay an action in javascript
I have got no experience in JS and I really need help. 我没有JS方面的经验,我真的需要帮助。 I have this code: 我有以下代码:
<script type="text/javascript">
var showElem;
showElem = function( showID ) {
div = (( document.getElementById ) ? document.getElementById( showID ) : document.all[ showID ] );
try {
div.className = (( div.className === "hide" ) ? "show" : "hide" );
} catch( e ) {
div.style.display = (( div.style.display === "none" ) ? "block" : "none" );
}
};
</script>
I need the action that make the div disappear to be delayed in 400 milliseconds. 我需要使div消失的动作要延迟400毫秒。
Thanks 谢谢
You could do it this way 你可以这样
<script type="text/javascript">
var showElem;
showElem = function( showID ) {
div = (( document.getElementById ) ? document.getElementById( showID ) : document.all[ showID ] );
try {
setTimeout(function() {
div.className = (( div.className === "hide" ) ? "show" : "hide" );
}, 400)
} catch( e ) {
div.style.display = (( div.style.display === "none" ) ? "block" : "none" );
}
};
</script>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.