EnvJS/Rhino, setTimeout() 不起作用



我目前在我的系统上设置了EnvJS(从这里安装(。我的最终目标是加载一个页面,让它的javascript进程几秒钟,然后阅读dom以获取感兴趣的信息。但是我无法让 setTimeout(( 来挽救我的生命(或 JQuery(。

我有一个启动该过程的 php 脚本:

...
$ENVJS_PATH = "/var/www/project/src/envjs";
$RHINO_JAR = "rhino/js.jar";
$INIT_SCRIPT = "init.js";
$output = shell_exec("java -jar $ENVJS_PATH/$RHINO_JAR -opt -1 $ENVJS_PATH/$INIT_SCRIPT");
echo "Response from javascript:<br/> $output";
...

init.js 文件如下所示:

load('/var/www/project/src/envjs/dist/env.rhino.js');
print("Loaded env.rhino.js<br/>");
// temporarily commented out
//var url = "http://www.ken-soft.com";
//window.location = url;
//print("<br/>Loaded "+url+"<br/>");
// Problem starts here
var runAfterPause=function() {
  print("got here..."); // never gets called
  print(document.getElementById('some_id').innerHTML);
}
setTimeout(runAfterPause, 3000); //wait three seconds before continuing
// i have also tried setTimeout("runAfterPause()", 3000);
print("<br/>End<br/>");

任何这方面的知识将不胜感激。谢谢。

尝试 env.rhino.1.2.js - 如果托管rhino的服务器操作系统是 Ubuntu,请尝试sudo apt-get install rhino - 并调用rhino -opt -1 ...而不是java -jar ...

当直接在 shell 上运行时,似乎在 Ubuntu 11.04 上对我来说是这样运行的 - 不确定 PHP 的shell_exec是否会影响事情。

编辑:确实它并没有真正起作用;我稍微浏览了一下源头,可以看到setTimeout依赖于Timer.prototype.start = function(){};,这显然是空的。进一步浏览,似乎唯一处理计时的是Envjs.wait() - 使用它,我终于可以得到一种定时循环;但是,请注意,它现在似乎是严格的单线程(同步(:

print("loading " + 1.2);
load('env.rhino.1.2.js'); // takes a while ...
print("loaded " + 1.2);
console.log(window);
var c=0;
function timedCount() // like this, when setTimeout calls a string!
{
  c=c+1;
  print("c=" + c);
  if (c<10) // make a limit for the run of script:
  {
    var t;
    //~ t=window.setTimeout(timedCount(),100); // TypeError: fn is not a function, it is undefined.
    t=window.setTimeout("timedCount()",1000); // must have `t=...` - else it locks on return even w/ wait(0)!
    Envjs.wait(); // waits, but "timer error  undefined   TypeError: fn is not a function, it is undefined." if setTimout doesn't call string; wait(0) exits immediately
  } else Envjs.wait(0); // "reset": execute all timers and return; else here will be left hanging from previous wait()
}

// main:
timedCount();
//~ eval("timedCount()", null); // works the same
print("after timedCount()");

。结果是:

$ sudo apt-get install rhino
$ wget https://github.com/thatcher/env-js
$ rhino -opt -1 test.js
loading 1.2
[  Envjs/1.6 (Rhino; U; Linux i386 2.6.38-11-generic; en-US; rv:1.7.0.rc2) Resig/20070309 PilotFish/1.2.13  ]
loaded 1.2
[Window]
a
c=1
c=2
c=3
c=4
c=5
c=6
c=7
c=8
c=9
c=10
after timedCount()

如果我没记错的话,在浏览器中setInterval是异步/多线程的 - 事实上,在浏览器中 JavaScript Shell 1.4,几乎相同的代码:

var c=0;
function timedCount() 
{
  c=c+1;
  print("c=" + c);
  if (c<10)  {
    var t;
    t=window.setTimeout("timedCount()",1000); 
  }
}
timedCount();
print("after timedCount()");

生产:

c=1
after timedCount()
c=2
c=3
c=4
c=5
c=6
c=7
c=8
c=9
c=10

回调方法是在赋值之后定义的。尝试将其放在设置超时之前

打印是一种窗口方法。

它用于打印页面,与打印机...

它可能与它和您的打印方法有冲突。

以下是我希望您尝试调试的 2 件事。至少在视觉上,我看不到你的代码有什么问题,除非有人看到它。

首先,当你自己调用函数时,你的函数能工作吗? 不在设置的超时内

var runAfterPause=function() {
  print("got here..."); 
  print(document.getElementById('some_id').innerHTML);
}
// call function by it self
runAfterPause();

第二次尝试在 setTimeout 中将其作为匿名函数运行;

var delay = setTimeout(function () {
   print("got here..."); // never gets called
   print(document.getElementById('some_id').innerHTML);
},3000);

这应该可以帮助您调试代码并查看错误所在。

最新更新