首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >EnvJS/Rhino,setTimeout()不工作

EnvJS/Rhino,setTimeout()不工作
EN

Stack Overflow用户
提问于 2011-05-30 05:56:32
回答 4查看 1.9K关注 0票数 1

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

我有一个php脚本来启动这个过程:

代码语言:javascript
复制
...
$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文件如下所示:

代码语言:javascript
复制
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/>");

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

EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2011-09-03 17:40:31

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

在Ubuntu11.04上,当直接在shell上运行时,我似乎是这样运行的--不确定PHP的shell_exec是否会影响事情。

编辑:事实上,它并不是真的有效;我看了一下源代码,可以看到setTimeout依赖于Timer.prototype.start = function(){};,它显然是空的。进一步浏览,似乎唯一处理计时的是Envjs.wait() -使用它,我终于可以得到一种计时循环;然而,请注意,它现在似乎是严格的单线程(同步):

代码语言:javascript
复制
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()");

..。结果是:

代码语言:javascript
复制
$ 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中,几乎相同的代码:

代码语言:javascript
复制
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()");

产生:

代码语言:javascript
复制
c=1
after timedCount()
c=2
c=3
c=4
c=5
c=6
c=7
c=8
c=9
c=10
票数 2
EN

Stack Overflow用户

发布于 2011-05-30 06:07:13

在赋值之后定义回调方法。试着把它放在setTimeout之前

票数 0
EN

Stack Overflow用户

发布于 2011-05-30 06:26:59

打印是窗口的一种方法。

它是用来打印一页的,用打印机...

它可能与您的print方法冲突。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/6170676

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档