javascript - setTimeOut 无法正常工作?

标签 javascript mobile titanium settimeout setinterval

Possible Duplicate:
function in setInterval() executes without delay

我正在尝试使用 setTimeOut 来调用函数每隔 X毫秒,但无论我将其设置为什么时间范围,它似乎都会崩溃并且计数得非常快?我做错了什么吗?

var Count = 0;
var GameRunning = 0;

var lblTimer = Titanium.UI.createLabel({
    color:'#999',
    text:'I am Window 1',
    font:{fontSize:20,fontFamily:'Helvetica Neue'},
    textAlign:'center',
    width:'auto',
    height: 25,
    top: 25
});

var btnStartGame = Titanium.UI.createButton({
   title: 'Start',
   width: 50,
   height: 25,
   top: 75
});

function RunGame () {
  Count++;
  lblTimer.text = Count;
  x = setTimeout(RunGame(), 100000);
}

function StartGame () {
  if(GameRunning==0)
  {
    GameRunning = 1;
    RunGame();
  }
    else
  {
    // Stop the game.
    GameRunning = 0;
    Count = 0;
  }
}

function GameTimerCount() {
    Titanium.API.info("inside GameTimerCount");
    Count++;
    lblTimer.text = "RUNNING: " + Count;
}

btnStartGame.addEventListener('click',function(e)
{
    Titanium.API.info("button clicked");
    StartGame();

    //var test = setInterval("GameTimerCount()",100);
});

// Add objects to window
Titanium.UI.currentWindow.add(lblTimer);
Titanium.UI.currentWindow.add(btnStartGame);

最佳答案

将 setTimeout 调用更改为:

setTimeout(RunGame,1000);

RunGame() 返回 void,因此 setTimeout(RunGame(),1000); 实际上更类似于 setTimeout(void,1000); 不执行任何操作。 setTimeout 的工作原理是使用函数指针来了解要执行的函数。

关于javascript - setTimeOut 无法正常工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8259665/

相关文章:

javascript - 设置 HTML/CSS 背景颜色

javascript - 尝试理解 Javascript 箭头函数

java - 如何从 vcard 中获取电话号码

android - 我们是否需要下载移动应用程序的 SSL 证书才能访问安全的网络服务

node.js - npm 将 web3 安装到 Appcelerator Titanium SDK > 6.x 中

ios - 如何在应用程序中使用钛的所有选项打开默认相机?

ios - Titanium - 在 iOS native 模块中包装 ADTECHMobileSDK 的 CoreData 错误

javascript - 为什么我们将 null 传递给 XMLHttpRequest.send?

java - Nashorn:如何选择要调用的构造函数

Android Studio Android 模拟器无法启动。