尝试每秒循环执行 [英] Trying to execute while loop every second

查看:89
本文介绍了尝试每秒循环执行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要这些while循环来执行每秒而不是瞬时执行。每当我加载机器人操作系统并使deltaX / Y大于20时,它就会跳到1000000.我希望它从1到1000000以每秒1步的速度运行。有人可以帮我这个。谢谢



< pre lang =  使用Javascript> 
var xmax = 1000000 ;
var ymax = 1000000 ;
var negxmax = -1000000;
var negymax = -1000000;
var x = 1 ;
var y = 1 ;

while (joystick.deltaX()> 20 && x< xmax){
x = x + 1 ;
}
while (joystick.deltaX()< - 20 & & x> negxmax){
x = x - 1
}
while (joystick.deltaY()> 20 && y< ymax){
y = y + 1 ;
}

while (joystick.deltaY()< -20&& y> negymax){
y = y - 1 ;
}

解决方案

首先,你没有任何与时间有关的东西。你不能执行一个任意的循环,并保证它需要1秒才能完成循环。



所以,问题不在于如何使循环工作,它实际上是如何在javascript中每秒执行一次代码的。为了这个目的,使用 setInterval



谷歌的 javascript执行代码每x秒 [ ^ ]。


使用 Window setInterval()方法 [< a href =http://www.w3schools.com/jsref/met_win_setinterval.asptarget =_ blanktitle =New Window> ^ ]。

调整以下内容示例代码:

 <  !DOCTYPE     html  >  
< html >
< 正文 >
< script >
var xmax = 5;
var x = 0;
var myTimer = setInterval(myFunction,1000); // start timer
function myFunction(){
if(x < xmax ){

document.write(++ x + '< br > ');
} else {
clearInterval(myTimer); //停止计时器
}
}
< / script >
< / body >
< / html >


除了解决方案1.



很多时候,更新的方法更好,特别是在动画/游戏中: window.requestAnimationFrame 。请参阅: http://paulirish.com/2011/requestanimationframe-for-smart-animating [< a href =http://paulirish.com/2011/requestanimationframe-for-smart-animatingtarget =_ blanktitle =New Window> ^ ]。



请查看我的游戏实现,您可以在其中找到该技术的一些有趣细节:画布上的俄罗斯方块 [ ^ ]。



另请注意使用 setTimeout 作为后备。这是为了以防万一。无论如何,当今非废话浏览器的所有JavaScript引擎都将实现 requestAnimationFrame



- SA

I need these while loops to execute every second instead of it being instantaneous. Whenever I load up robot operating system and make deltaX/Y greater than 20 it will just jump to a 1000000. I want it to go from 1 to 1000000 going in steps of 1 every second. Can someone please help me with this. Thanks

<pre lang="Javascript">
var xmax = 1000000;
var ymax = 1000000;
var negxmax = -1000000;
var negymax = -1000000;           
var x = 1;
var y = 1;

while (joystick.deltaX() > 20 && x < xmax ){
            x = x + 1;                   
        }
        while(joystick.deltaX() < - 20 && x > negxmax){
            x = x - 1
        }
        while (joystick.deltaY() > 20 && y < ymax){
            y = y + 1;      
        } 

        while (joystick.deltaY() < -20 && y > negymax){
            y = y - 1;
        }

解决方案

First, you have nothing in there that is time dependent. You cannot execute an arbitrary loop and guarantee that it's going to take 1 second to go through the loop.

So, the question is NOT how to make there loops work, it's really how do I execute code once a second in javascript. For that look into using setInterval.

Google for "javascript execute code every x seconds[^]".


Use Window setInterval() Method[^].
Adapt the following sample code:

<!DOCTYPE html>
<html>
<body>
<script>
var xmax = 5;
var x = 0;
var myTimer = setInterval(myFunction, 1000); // start timer
function myFunction() {
   if (x < xmax ){

       document.write(++x + '<br>') ;
   } else {
       clearInterval(myTimer);  // stop timer
   }
}
</script>
</body>
</html>


In addition to the Solution 1.

Very often, newer approach is better, especially in animation/gaming: window.requestAnimationFrame. Please see: http://paulirish.com/2011/requestanimationframe-for-smart-animating[^].

Please look at my game implementation where you can find number of interesting detail of the technique: Tetris on Canvas[^].

Also note the use of setTimeout as a fallback. This is done just in case. Anyway, all JavaScript engines of present-day non-nonsense browsers will implement requestAnimationFrame.

—SA


这篇关于尝试每秒循环执行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆