1

我目前在我的系统上设置了 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/>");

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

4

4 回答 4

2

尝试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
于 2011-09-03T09:40:31.197 回答
0

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

于 2011-05-29T22:07:13.390 回答
0

print 是一种窗口方法。

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

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

于 2011-05-29T22:26:59.400 回答
0

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

首先,当您自己调用它时,您的函数是否起作用?不在设定的超时范围内

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

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

于 2011-05-30T02:23:10.270 回答