Попробуйте env.rhino.1.2.js - и если серверная ОС, на которой размещен rhino
, - это Ubuntu, то попробуйте sudo apt-get install rhino
- и вызовите rhino -opt -1 ...
вместо java -jar ...
Кажется, что для меня это работает на 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