Category: Watch


Super geek Paul Irish posted an excellent video about his finding from the (not so) magical, jQuery source. If you have yet to indulge yourself in syntactical art, take a look at the source: bit.ly/jqsource, it’s not as scary as you think it is. Among the amazing things he goes over, I found the setInterval “loop” the coolest.

There are two JavaScript functions that are usually grouped together when doing anything with timing: setInterval and setTimeout. As their name suggests, setInterval executes a piece of code in intervals. setTimeout excecutes a piece of code after a certain amount of time. Each one is pretty awesome in it’s own right, however the way you use it can really bring out it’s true awesomeness. Thankfully, they use the same code format:

setTimeout(function(){alert("POW!")}, 1000);

setInterval(function(){alert("BANG!")}, 1000);

Each function takes two parameters (code, delay).
NOTE: The delay is in milliseconds, so  5 seconds = 5000.

Oftentimes, it’s necessary to execute a piece of code repeatedly. For example, lets say that in order for your application to start, you need to make sure that the application is connected to the Internet. You want to check more than once because the user may be having connection issues; their connection may not be immediately available when they start your app, but rather it may take about 5 seconds for a valid connection to establish.

You can use setInterval() to continuously check the connection every millisecond. Once a connection is established, your app can run. You could use setInterval(). Or, your can be a Badass Super Power Ninja and check this Zef knowledge Paul drops.

Let’s say that you want to use setInterval to perform some action at an interval (hence the name!). What if the function you want to execute takes longer than your interval? Take this awful example as an example:

setInterval(function(){
alert('yayQuery!');
}, 500);

For arguments sake, lets say that this alert will take over 3 seconds. This means that every half second your function will execute. But if your alert takes 3 seconds… the interval is set for every half second… what is an engine to do?? Crash, thats what (,,,will happen, eventually). Now what?

Use setTimeout recursively like so:

function beAwesome(){
alert("I am awesome");
}
(function doIt(){
beAwesome();
setTimeout(doIt, 500);
})();

Let’s break this down:

We first have  beAwesome(),a function that I want to continuously run in an interval. I then have a self invoking function (a function that immediately runs) that calls my beAwesome() function. After half a second, it calls itself (doIt()) to do it (execute) all over again.

The beauty about this technique is that no matter how long beAwesome() takes to execute, it won’t fudge up your process. Since it’s a linear execution, within the doIt() function, beAwesome() is called and executed. AFTER it’s done doing what it needs to do (which might take longer than half a second), it then calls it’s parent (doIt()) to rerun the code again.

Make sense? Of course it does!

For your brain:

Advertisements

John Lennon on Google

Thank you for sharing John Lennon with the world, Google.

We all know that Google is asearch engine giant. In fact, they’re more like a behemoth on steroids when it comes to sorting information. Aside from their extraordinary free web applications like Gmail, Earth, Maps, Talk, Checkout, Docs, (you get the idea)… they also know how to communicate to their audience: everyone.

If you have a Gmail account, then you may have recently noticed a beta version of a new feature called Priority Inbox. It’s an innovative way to filter out your incoming messages by providing labels that set the importance of the message (not that you don’t mind sorting your penis enhancement and Egyptian gold rush opportunities fifty percent of the time you are reading your inbox).

The thing that I enjoy most about this new service that Google has bestow upon us, is the way they explain how it works. If you’re like me, you want to play with it the moment it comes out of the box; forget the instructions, you want to figure it out on your own. Then twenty minutes later you are left with a couple of screws and a washer and think to yourself, “I’m THAT awesome.”

Instructions help people do things right and cartoons make things awesome.

On a side note, check out the recently released YouTube Instant by http://www.feross.org/; very inspiring.

http://www.yuiblog.com/blog/2010/08/30/yui-theater-douglas-crockford-crockford-on-javascript-scene-6-loopage-52-min/

Previous Videos: http://yuiblog.com/crockford/

In case you missed it, here’s the webcast:

Free jQuery webcast

Today ONLY!!
http://www.oreillynet.com/pub/e/1649

Update:

Check out the video here: https://qpants.wordpress.com/2010/07/22/oreilly-webcast-5-jquery-mistakes-youre-probably-making-in-your-project/