Search code examples
javascripthtmldelayhiddenappearance

how to delay an action in javascript


I have got no experience in JS and I really need help. 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.

Thanks


Solution

  • 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>