14.08.2016 Views

Beginning JavaScript with DOM Scripting and Ajax, 2nd Edition

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Chapter 6 ■ Common Uses of <strong>JavaScript</strong>: Images <strong>and</strong> Windows<br />

Figure 6-13. Changing the window position <strong>and</strong> dimension<br />

Animation <strong>with</strong> Window Intervals <strong>and</strong> Timeouts<br />

You can use the setInterval() <strong>and</strong> setTimeout() window methods to allow for the timed execution of code.<br />

setTimeout means you wait a certain time before executing the code (once only); setInterval() executes the code<br />

every time the given time period has passed.<br />

• name = setInterval('someCode', x ): Executes the <strong>JavaScript</strong> code passed to it as<br />

someCode every x milliseconds<br />

• clearInterval( name ): Cancels the execution of the interval called name (keeps the code<br />

from being executed again)<br />

• name=setTimeout('someCode', x ): Executes the <strong>JavaScript</strong> code someCode once, after<br />

waiting x milliseconds<br />

• clearTimeout( name ): Stops the timeout called name if the code hasn’t yet been run<br />

■ ■Note The parameter someCode in setInterval() <strong>and</strong> setTimeout() is a function you’ve defined.<br />

182<br />

www.it-ebooks.info

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!