EnvJS/Rhino,setTimeout() 不起作用

发布于 2024-11-10 12:36:01 字数 1132 浏览 0 评论 0原文

我目前在我的系统上设置了 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

聆听风音 2024-11-17 12:36:01

尝试 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() - 使用它,我终于可以得到一种定时循环;但是,请注意,现在它似乎是严格的单线程(同步):

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

Try env.rhino.1.2.js - and if the server OS hosting rhino is Ubuntu, then try sudo apt-get install rhino -- and call rhino -opt -1 ... instead of java -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 on Timer.prototype.start = function(){};, which is apparently empty. Browsing further, the only thing that seems to deal with timing is Envjs.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:

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()");

... and the results are:

$ 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()

If I recall correctly, in a browser setInterval is asynchronous/multithreaded - indeed, in a browser JavaScript Shell 1.4, the almost same code:

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()");

produces:

c=1
after timedCount()
c=2
c=3
c=4
c=5
c=6
c=7
c=8
c=9
c=10
远昼 2024-11-17 12:36:01

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

The callback method is defined after the assignation. Try putting it before setTimeout

情徒 2024-11-17 12:36:01

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.

猫弦 2024-11-17 12:36:01

我希望您尝试以下两件事进行调试。至少在视觉上我看不到代码中的错误,除非有人看到它。

首先,当您单独调用它时,您的函数是否可以工作?不在 set Timeout 内

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);

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

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

var runAfterPause=function() {
  print("got here..."); 
  print(document.getElementById('some_id').innerHTML);
}

// call function by it self
runAfterPause();

2nd try running it as an anonymous function inside your setTimeout;

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

this should help you debug your code and see where the error is.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文