Sam*_*iri 0 javascript mobile settimeout titanium setinterval
可能重复:
setInterval()中的函数无延迟地执行
我试图每隔X几毫秒使用setTimeOut来调用calla函数,但它似乎只是吓坏了,无论我设置的时间范围如何都非常快?难道我做错了什么?
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);
Run Code Online (Sandbox Code Playgroud)
将setTimeout调用更改为:
setTimeout(RunGame,1000);
Run Code Online (Sandbox Code Playgroud)
RunGame()返回void,所以setTimeout(RunGame(),1000);实际上更像是setTimeout(void,1000); 什么都没有.setTimeout通过使用函数指针来知道要执行的函数.
| 归档时间: |
|
| 查看次数: |
7944 次 |
| 最近记录: |