【问题标题】:Adding timer in Titanium mobile application在 Titanium 移动应用程序中添加计时器
【发布时间】:2012-09-18 12:28:37
【问题描述】:

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

感谢

【问题讨论】:

    标签: titanium appcelerator titanium-mobile appcelerator-mobile


    【解决方案1】:

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

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

    区别在于setInterval 重复和setTimeout 执行一次。

    【讨论】:

      【解决方案2】:
      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();
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多