0

I'm trying to understand where to add code to delay the toggle of my div by 0.5 seconds. I am a beginner with JavaScript/jQuery.

function toggle_visibility(id) {
       var e = document.getElementById(id);
       if(e.style.display == 'block')
          e.style.display = 'none';
       else
          e.style.display = 'block';
    }

//it is tied to this if it can help

<a onclick="toggle_visibility('section1_1_content');toggle_visibility('note1_1');"> <div id="help_tiem_section1_1" onclick="chngimg1()" onmouseover="this.style.cursor='pointer'">
            <p1>TEST</p1><img src="down.png" height="10px" width="15px" id="imgplus1"/>
        </div></a>

This works. However, I'd like to change it so that when it changes back to display none it does not delay.

3
  • 1
    developer.mozilla.org/en-US/docs/Web/API/… Commented Jan 12, 2015 at 14:26
  • 2
    Try using Google.... Commented Jan 12, 2015 at 14:27
  • I put the subject title into google and came up with plenty of results. Please put a little research effort in before asking questions Commented Jan 12, 2015 at 14:28

2 Answers 2

3

use setTimeout():

setTimeout(yourFunction, 500)
Sign up to request clarification or add additional context in comments.

Comments

0

You should use setTimeout

function toggle_visibility(id) {
   setTimeout(function() {
       var e = document.getElementById(id);
       if(e.style.display == 'block')
          e.style.display = 'none';
       else
          e.style.display = 'block';
    }, 500);
}

Update:

function toggle_visibility(id) {
    var e = document.getElementById(id);
    if (e.style.display == 'block') {
        setTimeout(function() {
            e.style.display = 'none';
        }, 500);
    } else {
        e.style.display = 'block';
    }
}

4 Comments

I think that never will run, you are missing );
@WilfredoP added missing characters.
@jcubic This works however how do I change it so that when it changes back to display none it does not delay. Thanks a lot!
@joey simply wrap only one if branch with setTimeout.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.