Creating Timed Events in Your iPhone App with Titanium

Creating Timed Events in Your iPhone App with 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.

Tags: , , , ,

I am a web entrepreneur, internet addict, e-commerce lover, and music fan. I currently work for a Internet Retailer 500 E-commerce site, managing email marketing, social media, and mobile marketing. In the past, I worked as an Internet Marketing Director, Design Director, Web Developer, and Product Designer.

1 Comment Leave yours

  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

Leave a Reply