Creating Timed Events in Your iPhone App with Titanium

Timers are core elements of games. I’ll show you how to write a basic timer function that will let you trigger an event on an interval for your iphone app, using Appcelerator’s Titanium.

Since Titanium allows you to write code in Javascript, you can write functions to do a bunch of stuff for you and make writing your app much easier. I wrote a simple timer function that can be used to fire other events within your app. This might be useful for games, cooking apps, etc.

So here’s the code:

function setTimer(timetowait,context) {
 
mt=0;
mtimer = setInterval(function() {
 
    	mt++;
	Ti.API.info(mt);
 
    if(mt==timetowait) {
        // do something;
	clearInterval(mtimer);
 	Ti.App.fireEvent(context);
        }
 
},1000);
};

So let’s break it down to understand what it does. First let’s look at the variable we pass into the function: timetowait and context

timetowait simply represents the number of seconds the timer should count to until you want your event to fire.

context is the name of the event that you want to fire when the timer reaches timetowait

Ti.API.info(mt) simply writes out the Titanium debug window the value of mt, which is our variable that increments every second. This is optional, but helpful in the development process to see exactly when the timer stops.

clearInterval(mtimer) tells the setInterval function in Titanium to essentially stop/reset.

Ti.App.fireEvent(context) fires the name of the event we passed in as the variable context.

Let’s look at a couple of examples of how this could be used. Let’s say we have a basic game where the player has to do something in a specific amount of time. We could use our setTimer function to time the game, and then show a game over alert message in the app:

//set the timer for 30 seconds, and after that show the game over message
setTimer(30, 'showGameOver');
 
//create an event listener for our Game Over alert
Ti.App.addEventListener("showGameOver", function() {
	alert("Game Over!");
});

That’s a pretty simple example. Let’s say we wanted to continuously loop the timer:

//set the timer for 30 seconds, and then call the timer function again
setTimer(30, 'loopTimer');
 
Ti.App.addEventListener("loopTimer", function() {
 
	//call the timer function again
	setTimer(30, 'loopTimer');
});

In both of these cases we can add as much complexity to our event listeners (the event that is fired when the timer ends) as we want…we could update the on-screen display, fire off multiple events, write data to the local database, load a remote file, check network connectivity, etc.

Timers can be used for all sorts of things in iPhone apps. Hope this helps someone get started using them.

3 thoughts on “Creating Timed Events in Your iPhone App with Titanium

  1. Vassilis

    Hello Brian,

    I’ve tried to implement a similar timer to fire every second. The timer works pretty well in the foreground but not in the background. Sometimes it runs OK (save a couple of seconds lost per hour – I guess related to app switching) sometimes it slips behind a lot. The idea is to have a timer that will count down to zero and notify the user accordingly, even if the app is in the background or the iphone is locked. Any ideas?

    Thanks

    Vassilis

  2. Terra

    I see you share interesting things here, you can earn some extra cash, your blog
    has huge potential, for the monetizing method, just search in google – K2
    advices how to monetize a website

  3. Junko

    I read a lot of interesting articles here. Probably you spend a lot of time writing,
    i know how to save you a lot of time, there is an online tool that
    creates high quality, SEO friendly articles in seconds, just type in google – laranitas free
    content source

Leave a Reply

Your email address will not be published. Required fields are marked *