Strange issue here, I don't see why it isn't working. But basically the alert is fire as soon as I press the button, there is no 5 second delay at all!
<html>
<head>
<title>Testing Page</title>
</head>
<script type="text/javascript">
function testing() {
var delay = 5000;
setTimeout(alert("5 seconds later..."),delay);
}
</script>
<body>
<input type="button" value="Run Function" onClick="testing()">
</body>
</html>
Strange issue here, I don't see why it isn't working. But basically the alert is fire as soon as I press the button, there is no 5 second delay at all!
<html>
<head>
<title>Testing Page</title>
</head>
<script type="text/javascript">
function testing() {
var delay = 5000;
setTimeout(alert("5 seconds later..."),delay);
}
</script>
<body>
<input type="button" value="Run Function" onClick="testing()">
</body>
</html>
Share
asked Jan 13, 2014 at 2:18
Martyn BallMartyn Ball
4,9059 gold badges63 silver badges145 bronze badges
2 Answers
Reset to default 8function testing() {
var delay = 5000;
setTimeout(function(){alert("5 seconds later...");},delay);
}
Need to wrap it in a function so the alert is not immediately executed.
Check out the MDN reference for more information regarding how to use setTimeout
The setTimeout function is used as follows:
setTimeout(<function>, <delay>);
The first parameter is a function. What you're doing is giving it the value of alert(..);
Change it to:
setTimeout(function(){ alert("5 seconds later..."); }, delay);
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745069104a4609436.html
评论列表(0条)