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/>");
任何有关此的知识将不胜感激。谢谢。
I currently set up EnvJS on my system (installed from here).
My end goal is to load a page let it's javascript process for a few seconds, and then read the dom to get the information of interest. However I can not get setTimeout() to work to save my life (Or JQuery for that matter).
I have a php script that starts the process:
...
$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";
...
the init.js file looks like:
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/>");
Any knowledge on this would be much appreciated. Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
尝试 env.rhino.1.2.js - 如果服务器操作系统托管
rhino
是 Ubuntu,然后尝试sudo apt-get install rhino
- 并调用rhino -opt -1 ...
而不是java -jar ...
在 Ubuntu 11.04 上,当直接在 shell 上运行时,对我来说似乎是这样运行的 - 不确定 PHP 的
shell_exec
是否会影响事物。编辑:确实它并没有真正发挥作用;我稍微浏览了一下源代码,可以看到
setTimeout
依赖于Timer.prototype.start = function(){};
,它显然是空的。进一步浏览,似乎唯一处理计时的是Envjs.wait()
- 使用它,我终于可以得到一种定时循环;但是,请注意,现在它似乎是严格的单线程(同步):...结果是:
如果我没记错的话,在浏览器中
setInterval
是异步/多线程的 - 事实上,在浏览器 JavaScript Shell 1.4,几乎相同的代码:产生:
Try env.rhino.1.2.js - and if the server OS hosting
rhino
is Ubuntu, then trysudo apt-get install rhino
-- and callrhino -opt -1 ...
instead ofjava -jar ...
Seems to run like this for me on Ubuntu 11.04, when ran directly on the shell - not sure if PHP's
shell_exec
may influence things or not..EDIT: Indeed it is not really working; I looked through the source a bit, and could see that
setTimeout
relies onTimer.prototype.start = function(){};
, which is apparently empty. Browsing further, the only thing that seems to deal with timing isEnvjs.wait()
- and using that, I can finally get a sort of a timed loop; however, note that it seems to be strictly single-threaded (synchronous) now:... and the results are:
If I recall correctly, in a browser
setInterval
is asynchronous/multithreaded - indeed, in a browser JavaScript Shell 1.4, the almost same code:produces:
回调方法是在赋值之后定义的。尝试将其放在 setTimeout 之前
The callback method is defined after the assignation. Try putting it before setTimeout
print是window的一种方法。
它是用来打印一页的,用打印机...
它可能和你的打印方法有冲突。
print is a method of window.
It's used to print a page, with a printer...
There might be a conflict with it and your print method.
我希望您尝试以下两件事进行调试。至少在视觉上我看不到代码中的错误,除非有人看到它。
首先,当您单独调用它时,您的函数是否可以工作?不在 set Timeout 内
第二次尝试将其作为 setTimeout 内的匿名函数运行;
这应该可以帮助您调试代码并查看错误所在。
Here are 2 things i want you to try for debugging. At least visually i cannot see something wrong in yout code, unless someone see it.
first does your function work when you call it by itself? not inside the set Timeout
2nd try running it as an anonymous function inside your setTimeout;
this should help you debug your code and see where the error is.