Executing JavaScript after X seconds

前端 未结 3 427
天涯浪人
天涯浪人 2020-11-30 04:41

I am building a interstitial page, using

and JavaScript, really simple script but neat.

Everything is working, but I also would like to close the div\'s aft

相关标签:
3条回答
  • 2020-11-30 05:06

    setTimeout will help you to execute any JavaScript code based on the time you set.

    Syntax

    setTimeout(code, millisec, lang)
    

    Usage,

    setTimeout("function1()", 1000);
    

    For more details, see http://www.w3schools.com/jsref/met_win_settimeout.asp

    0 讨论(0)
  • 2020-11-30 05:16
    onclick = "setTimeout(function() { document.getElementById('div1').style.display='none';document.getElementById('div2').style.display='none'}, 1000)"
    

    Change 1000 to the number of milliseconds you want to delay.

    0 讨论(0)
  • 2020-11-30 05:19

    I believe you are looking for the setTimeout function.

    To make your code a little neater, define a separate function for onclick in a <script> block:

    function myClick() {
      setTimeout(
        function() {
          document.getElementById('div1').style.display='none';
          document.getElementById('div2').style.display='none';
        }, 5000);
    }
    

    then call your function from onclick

    onclick="myClick();"
    
    0 讨论(0)
提交回复
热议问题