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.

halfer
  • 19,824
  • 17
  • 99
  • 186
joey
  • 9
  • 1

2 Answers2

3

use setTimeout():

setTimeout(yourFunction, 500)
BeNdErR
  • 17,471
  • 21
  • 72
  • 103
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';
    }
}
jcubic
  • 61,973
  • 54
  • 229
  • 402