bbb Home |
Lesson 9 - Periodically Move Target |
SUMMARYIn this lesson you will learn how to use a timer to move the target at periodic intervals. DISCUSSIONThe window object has many methods. You have already learned about some of these such as window.alert() and window.open(). Another method is setInterval(). This method causes an action to occur at specified interval of time: timerName = window.setInterval("action", interval)
Once a timers starts, it will execute the action at the specified interval until the timer is turned off. A timer can be turned off using the clearInterval method: window.clearInterval(timerName) The example below illustrates the use of the setInterval method. On lines 11 to 13, <script> is embedded in the <body>. When the page loads, this <script> executes. On line 12, a timer called timerX is started using the setInterval method. You could use just about any name for the timer. The action in this example is alert('hi'). This action will occur every 2000 milliseconds (2 seconds). Lines 4 to 7 define a function that turns off timerX using the clearInterval method. This function is called on line 15 in the onclick event for the button. EXAMPLE - window.setInterval("action", interval)
PRACTICEIn the example above:
GAME DEVELOPMENTModify your game page (game.htm) as follows:
|
References |
©dfx interactive, inc. Contact us at bigbadbrain@bigbadbrain.com |