How to set timer on body onload?

后端 未结 3 1982
旧时难觅i
旧时难觅i 2021-01-27 06:18

I want to set timer on body onload eventHandler to call function where I show hide div.


    
3条回答
  •  北恋
    北恋 (楼主)
    2021-01-27 06:26

    • For better readability, I've put the code in a separate function.
    • I've added an id to the div for easier selection through javascript.
    • In the onload attribute, I'm calling setInterval, which gets an anonymous function as a first parameter.
    • This function will be called every time after the interval (1000 ms) elapses.
    • The function will then select the element with the id theDiv, and set its css display attribute to block, or none (depending on the previous value).

    JSFiddle for demonstration: http://jsfiddle.net/GAE8L/1/

    Note that the first parameter for setInterval is just the function name, not a function call (so no parenthesis!):

    
        
    
    
    
    

提交回复
热议问题