在Titanium移动应用程序中添加计时器

时间:2012-09-18 12:28:38

标签: titanium appcelerator titanium-mobile appcelerator-mobile

我想在我的移动应用程序中添加一个计时器,我正在使用Titanium框架进行开发。我没有在文档中找到任何相关的东西。任何人都可以为这个问题提出解决方案。

感谢名单

2 个答案:

答案 0 :(得分:10)

如果您的意思是稍后执行代码的计时器,只需使用javascript setTimeoutsetInterval

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

差异为setInterval重复,setTimeout执行一次。

答案 1 :(得分: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();