I want to set timer on body onload eventHandler
to call function where I show hide div.
div
for easier selection through javascript.onload
attribute, I'm calling setInterval
, which gets an anonymous function as a first parameter.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!):
<body onload="setInterval(onTimerElapsed, 1000);">
<div id="theDiv" style="display:none;">
hide
</div>
</body>
<script type="text/javascript">
function onTimerElapsed() {
var myDiv = document.getElementById('theDiv');
myDiv.style.display = myDiv.style.display === 'none' ? 'block' : 'none';
}
</script>
Use jquery, it less..
function foo() { // method show should be called. $("#idDiv").show(); } function bar() { setTimeout(foo(), 1000) } </script> </code> <body onload="bar()"> </body>
Try this:
<script src="http://code.jquery.com/jquery-latest.min.js"></script>
<div id="div1" style="display:none;">
hide
</div>
<script>
$(function() {
setTimeout(function() {
$('#div1').show();
}, 1000);
});
</script>