Skip to the content...

Welcome to the David Walsh Blog. I'm a MooTools, Dojo, jQuery, CSS, and PHP Web Developer located in Madison, Wisconsin, United States. Please contact me if I can make your experience on my website better.

Using MooTools Periodicals

17 Responses »

If I were to explain MooTools in a sentence I'd say "MooTools makes common JavaScript tasks exponentially easier and the code more elegant." Dealing with intervals in JavaScript is fairly simple but not so elegant. Here's MooTools's take on setInterval.

Basic Function.periodical Usage

/* the function to repeatedly run */
var fnToRepeat = function() {
	console.log('Running periodical!');
};
/* set periodical into action!  once every second */
var periodicalID = fnToRepeat.periodical(1000);

The above function will run periodically every second. This method takes the place of JavaScript's native setInterval -- periodical Moo-izes setInterval. You'll also notice that this method returns the interval ID.

Clearing Periodicals with $clear

/* the function to repeatedly run */
var count = 0;
var fnToRepeat = function() {
	count++;
	console.log('Periodical run ' + count + ' times');
	if(count == 10) {
		$clear(periodicalID);
	}
};
/* do only 10 times, once every second */
var periodicalID = fnToRepeat.periodical(1000);

When you no longer want the periodical to run, you simply use MooTools' global $clear method. That's it -- the periodical stops for good. You may initialize another periodical with the same function however a new interval ID will be returned.

Real-Life Example

I was required to write a slideshow of sorts for a client which would periodically swap slides. The periodical was meant to continue "forever" but stop when the mouse hovered other the displaying slide, then start again when the mouse left. Here's how I did it:

/* setup */
var periodicalID;
var begin = function() {
	periodicalID = (function() {
		//do all the rotation stuff here
	}).periodical(5000);
}
/* start it! */
begin();
/* events ("start" / "stop") */
$('slider-container').addEvents({
	mouseenter: function() {
		//temporarily stop
		$clear(periodicalID);
	},
	mouseleave: begin
});

Note that I didn't "pause" the periodical per say -- I simply stopped the periodical and started a new interval when the mouse left the slide area.

Periodicals are an essential functionality in JavaScript that MooTools has more elegantly presented. MooTools FTW!

Discussion

  1. November 17, 2009 @ 9:31 am

    And let’s not forget periodical’s little brother, fn.delay(t) as an alternative to setTimeout(fn, t)…

  2. November 17, 2009 @ 9:34 am

    @Chris the Developer: Like any good little brother, fn.delay() wants his own post! :)

  3. November 17, 2009 @ 11:25 am

    ah, how the young quibble for a spot in the halls of fame :D

  4. November 17, 2009 @ 11:34 am

    mootools rocks!!

  5. November 17, 2009 @ 12:01 pm

    @Adriaan: This is correct.

  6. November 17, 2009 @ 4:30 pm

    In one of my websites, I use the new Date().getMinutes() and getHours() to update a counter every 10 seconds (10000ms). The fun is, mootools makes it easy, much more easy than with jQuery or basic Javascript.

    To keep in the spirit of Adriaan, indeed it rocks!

  7. November 18, 2009 @ 3:30 am

    Great stuff as always David

    Short of adding a counter to the function, is there any way to find out how far into a periodical you are before or when you $clear it? This way when you reset the periodical you can set it to the remaining time it had before!?

  8. November 18, 2009 @ 5:01 am

    Great example, thanks! I’m sure this will be helpful quite soon.

  9. November 18, 2009 @ 8:33 am

    This is insane. SUCK ME!

  10. November 18, 2009 @ 8:35 am

    @Jill: No, it’s very simple!

  11. November 21, 2009 @ 2:24 am

    For the same reason (slideshows) I created `Loop` to handle starting/stopping etc. of a periodical in a class. Check it: http://moodocs.ryanflorence.com/rpflo/Loop

    It has been more valuable than I ever imagined at first.

  12. ryan florence
    November 24, 2009 @ 1:51 pm

Be Heard!

Share your thoughts with fellow developers of all skill levels! I want to hear from you!

Name*:
Email*:
Website:  
Wrap your code with <code> tags, f00!