0

I've this code:

function divHideShow(divToHideOrShow) 
{
    var div = document.getElementById(divToHideOrShow);

    if (div.style.display == "none") 
    {
        div.style.display = "block";
    }
    else 
    {
        div.style.display = "none";
    }

}       

How to make it wait 2 seconds when executing div.style.display = "block"; ?

Thanks

2
  • 1
    Do you want to wait 2 seconds before setting to block, or after? Commented Jun 3, 2013 at 14:55
  • 1
    Then the answers here should work for you. Commented Jun 3, 2013 at 16:58

2 Answers 2

3

You can use the function setTimeout to wait for a specified time (in milliseconds) before running some code.

function divHideShow(divToHideOrShow) 
{
    var div = document.getElementById(divToHideOrShow);

    if (div.style.display == "none") 
    {
        setTimeout(function () {
            div.style.display = "block";
        }, 2000);
    }
    else 
    {
        div.style.display = "none";
    }

}

It might be worth considering adding and removing a class rather than setting the style directly. You can then handle all styles using CSS which helps with maintainability (and you can use CSS3 animations to fade/grow/shrink things in and out too).

Sign up to request clarification or add additional context in comments.

3 Comments

display=none on the element directly seems fine to me, but I'd say instead of doing display=block to re-show it, just clear the display style. That way you can define other display types in CSS (inline-block, float, etc) and this won't clobber it.
Thanks iblamefish! :-) It does its job!
@Recif when you think it answers your question... you should accept the answer so that answerer gets some encouragement
2

This answers you question: the second parameter of setTimeout takes milliseconds so, 2000 milliseconds= 2 seconds

function divHideShow(divToHideOrShow) 
{
    var div = document.getElementById(divToHideOrShow);

    if (div.style.display == "none") 
    {
        setTimeout(function(){div.style.display = "block";},2000);

    }
    else 
    {
        div.style.display = "none";
    }

}  

If you need fadeIn or fadeOut effects you better consider using jquery library:

You need to include jquery library in order this to be working.....

function divHideShow(divToHideOrShow) 
{

     var time=600;  //milli seconds alter this for changing speed
     $("#"+divToHideOrShow).fadeToggle(time);

}  

11 Comments

No, that doesn't work at all. setTimeout will run the second argument after the timeout, it won't delay the rest of the code.
2000 microseconds == 0.002 seconds. That argument is milliseconds, where 2000 milliseconds == 2 seconds.
@Herms My badness, I typed microseconds though I intended milliseconds
how would it? I didn't implement it... I've suggested you :) I will add the example to the answer if you need though :)
@Recif do you know about jquery in first place?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.