3

I want to add a timer to my mobile application, which I'm developing using Titanium framework. I didn't find any related thing in documentation. Can anybody suggest a solution for this problem.

Thanx

Ammar
  • 1,811
  • 5
  • 26
  • 60

2 Answers2

10

If you mean a timer for executing code later, just use javascript setTimeout or setInterval.

setTimeout(function(){
   toDoLater();
}, 1000);

difference being setInterval repeats and setTimeout executes once.

hvgotcodes
  • 118,147
  • 33
  • 203
  • 236
0
var win = Titanium.UI.createWindow({
    title:"Setting Timers",
    backgroundColor:"#FFFFFF"
});

var setTimeoutLabel = Titanium.UI.createLabel({
    text:"Tap Below",
    width:120,
    height:48,
    top:64,
    left:12,
    textAlign:"left",
});

var setTimeoutButton = Titanium.UI.createButton({
    title:"setTimeout",
    height:48,
    width:120,
    bottom:12,
    left:12 
});

var setIntervalLabel = Titanium.UI.createLabel({
    text:"Tap Below",
    width:120,
    height:48,
    top:64,
    right:12,
    textAlign:"right"
});

var setIntervalSwitch = Titanium.UI.createSwitch({
    bottom:24,
    right:12,
    value:false
});

setTimeoutButton.addEventListener("click",function(e){
    if(!this.fired){//Prevent multiple fires of the timeout
        var t = setTimeout(function(){
            setTimeoutLabel.text = "Fired!";
            clearInterval(t);
            t = null;
        },2000);
        this.fired = true;
    }
});

setIntervalSwitch.addEventListener("change",function(e){
    if(e.value){
        var i = 0;
        this.timer = setInterval(function(){
            if(i%2){
                setIntervalLabel.text = "";
            }else{
                setIntervalLabel.text = "Bang!";
            }
            i++;
        },500);
    }else{
        clearInterval(this.timer);
        this.timer = null;
        i=0;
        setIntervalLabel.text = "Stopped";
    }
});

win.add(setTimeoutLabel);
win.add(setTimeoutButton);
win.add(setIntervalSwitch);
win.add(setIntervalLabel);

win.open();
Durul Dalkanat
  • 7,266
  • 4
  • 35
  • 36