Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/367.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript js setTimeout递归返回continue_Javascript_C++_Recursion_Return_Settimeout - Fatal编程技术网

Javascript js setTimeout递归返回continue

Javascript js setTimeout递归返回continue,javascript,c++,recursion,return,settimeout,Javascript,C++,Recursion,Return,Settimeout,我将使用这个用C编写的简单迷宫解算器程序作为javascript的示例: int is_free_place(int x,int y) { char ch[2]; if(x<1||x>80||y<1||y>20) return 0; gettext(x,y,x,y,ch); return ch[0]==32; // X or space } int solve(int x, int y, int x_end,int y_end) { int ans=0;

我将使用这个用C编写的简单迷宫解算器程序作为javascript的示例:

int is_free_place(int x,int y) {
 char ch[2];
 if(x<1||x>80||y<1||y>20)
  return 0;
 gettext(x,y,x,y,ch);
 return ch[0]==32; // X or space
}
int solve(int x, int y, int x_end,int y_end) {
 int ans=0;
 if(kbhit()) efn();
 gotoxy(x,y);
 putchar('.');
 delay(DELAY_TIME);
 if(x==x_end&&y==y_end)
  return 1;
 if(is_free_place(x-1,y))
  ans=solve(x-1,y,x_end,y_end);      //// <<<<< recursion starts here
 if(ans==0&&is_free_place(x+1,y))    //// can't continue to here if using setTimeout
  ans=solve(x+1,y,x_end,y_end);      //// because ans is not set
 if(ans==0&&is_free_place(x,y-1))
  ans=solve(x,y-1,x_end,y_end);
 if(ans==0&&is_free_place(x,y+1))
  ans=solve(x,y+1,x_end,y_end);
 if(ans==0){
  gotoxy(x,y);
  putchar(' ');
  delay(DELAY_TIME);
 }
 return ans;
}
void main(){
 clrscr();
 efn(); // reads maze from file, shows menu, calls solve()
}
int是空闲的位置(intx,inty){
char-ch[2];
if(x80 | | y20)
返回0;
gettext(x,y,x,y,ch);
返回ch[0]==32;//X或空格
}
整数解算(整数x,整数y,整数x_端,整数y_端){
int ans=0;
if(kbhit())efn();
gotoxy(x,y);
putchar('.');
延迟(延迟时间);
if(x==x\u-end&&y==y\u-end)
返回1;
如果(是自由的地方(x-1,y))

ans=solve(x-1,y,x_end,y_end);///
setTimeout
只运行一次。它的返回值是超时本身

var timeout = setTimeout(function () { console.log( "hi" ) }, 1000);
// timeout == some id
如果您选择使用
clearTimeout
方法清除超时,则可以清除超时

您可以使用递归
setTimeout
循环创建游戏循环

setTimeout(function call() {
     // Do code
     //...     
     setTimeout( call, 1000 / 60 ); // loop
}, 1000 / 60 ) // 60 fps

下面是一个javascript示例,说明如何使用
setTimeout
,以及如何使用
cleartimout
清除它

<html>
<head>
<script type="text/javascript">
var c=0;
var t;
var timer_is_on=0;

function timedCount()
  {
  document.getElementById('txt').value=c;
  c=c+1;
  t=setTimeout("timedCount()",1000);
  }

function doTimer()
  {
  if (!timer_is_on)
    {
    timer_is_on=1;
    timedCount();
    }
  }

function stopCount()
  {
  clearTimeout(t);
  timer_is_on=0;
  }
</script>
</head>
<body>

<form>
<input type="button" value="Start count!" onclick="doTimer()" />
<input type="text" id="txt" />
<input type="button" value="Stop count!" onclick="stopCount()" />
</form>

</body>
</html>

var c=0;
变量t;
var定时器_为_on=0;
函数timedCount()
{
document.getElementById('txt')。value=c;
c=c+1;
t=setTimeout(“timedCount()”,1000);
}
函数doTimer()
{
如果(!计时器打开)
{
定时器_为_开=1;
timedCount();
}
}
函数stopCount()
{
清除超时(t);
定时器_为_开=0;
}

它取自

我认为setInterval将完成以下工作:


将其编写为可以与setTimeout一起使用的东西将是一件非常重要的事情,特别是因为编写时,您需要获得递归调用的结果。我认为处理这种情况的最简单方法是使用某种承诺基础结构,如dojo和jQuery(或者自己编写)。我会更详细地介绍,但目前无法进一步研究。一个提示:我认为jQuery的
pipe
方法会在这里创造奇迹


我想到的另一个选择是重写它,使用一个堆栈来保存ans变量,可能还有一些东西表明当前函数调用的哪一部分需要返回。

我们不会为您编写所有的JS。告诉我们您有什么(希望比C版本更可读).我不需要JS中所有的C代码;)我只需要返回部分的想法(我知道setTimeout返回id,我说的是setTimeout调用的函数的返回)所以我可以做一个不同的,更大的项目…然后,我知道所有这些,这不是我需要的。我需要处理setTimeout内函数的返回…@Antonimo你不能返回任何东西(无论如何,有用)从传递到
setTimeout
@MattBall right的回调中,这就是为什么我试图找到一个解决方法,以获得与我给出的示例C代码类似的结果。这是一个循环,而不是递归“凌乱”我提出的解决方案实际上是使用一个堆栈,在调用setTimeout之前,我将局部变量添加到堆栈中,并将函数划分为“步骤”。它适用于迷宫解算器,但继续使用这个方法太混乱了,我需要真正的递归=)
  var int = setInterval(clock,1000);

  function clock()
  {
     var d=new Date();
     var t=d.toLocaleTimeString();
     document.getElementById("clock").value=t;
  }