3

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

谢谢

4

2 回答 2

10

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

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

不同之处在于setInterval重复并setTimeout执行一次。

于 2012-09-18T12:38:02.927 回答
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();
于 2015-09-21T11:59:18.047 回答