18.04.2016 Views

Professional JavaScript For Web Developers

javascript for learners.

javascript for learners.

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Chapter 5<br />

Books<br />

Be careful not to overuse the status bar and thereby making it a distraction. <strong>For</strong><br />

example, many sites still use the scrolling message code that scrolls text across the<br />

status bar. Not only is this a fairly useless trick, it’s also annoying, very unprofessional,<br />

and adds a very amateurish feeling that a <strong>Web</strong> site or <strong>Web</strong> application can do<br />

without. Because this book is all about professional <strong>JavaScript</strong>, the scrolling text<br />

code will not be covered. However, if you are interested in playing around with<br />

scrolling text, check out http://javascript.internet.com/scrolls/, where you<br />

can find a large number of these scripts.<br />

Intervals and timeouts<br />

146<br />

Java developers are familiar with the wait() method of objects, which causes the program to stop and<br />

wait a specified amount of time before continuing on to the next line of code. This is a very useful piece<br />

of functionality and, unfortunately, one that <strong>JavaScript</strong> doesn’t support. But all is not lost. You have a<br />

couple of ways around this issue.<br />

<strong>JavaScript</strong> supports timeouts and intervals, which effectively tell the browser when certain code should<br />

be executed: Timeouts execute the given code after a specified number of milliseconds; intervals execute<br />

the given code repeatedly, waiting a specified number of milliseconds in between.<br />

Setting a timeout is done using the window’s setTimeout() method. This method accepts two arguments:<br />

the code to execute and the number of milliseconds (1/1000 of a second) to wait before executing it. The<br />

first argument can either be a string of code (as would be used with the eval() function) or a pointer to a<br />

function. <strong>For</strong> example, both these lines display an alert after one second:<br />

setTimeout(“alert(‘Hello world!’) “, 1000);<br />

setTimeout(function() { alert(“Hello world!”); }, 1000);<br />

Of course, you can also reference a previously defined function:<br />

function sayHelloWorld() {<br />

alert(“Hello world!”);<br />

}<br />

setTimout(sayHelloWorld, 1000);<br />

When you call setTimeout(), it creates a numeric timeout ID, which is similar to a process ID in an<br />

operating system. The timeout ID is essentially an identifier for the delayed process should you decide,<br />

after calling setTimeout(), that the code shouldn’t be executed. To cancel a pending timeout, use the<br />

clearTimeout() method and pass in the timeout ID:<br />

var iTimeoutId = setTimeout(“alert(‘Hello world!’)”, 1000);<br />

//nevermind<br />

clearTimeout(iTimeoutId);<br />

You may be thinking to yourself, “Why would I define a timeout and then cancel it before it executes?”<br />

Consider the tooltips available in most applications today. When you move your mouse over a button, it

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

Saved successfully!

Ooh no, something went wrong!