Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/444.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在不使用setTimeout的情况下异步运行javascript函数?_Javascript_Rhino_Parallel Processing - Fatal编程技术网

如何在不使用setTimeout的情况下异步运行javascript函数?

如何在不使用setTimeout的情况下异步运行javascript函数?,javascript,rhino,parallel-processing,Javascript,Rhino,Parallel Processing,它是一个服务器端Javascript(rhino引擎),因此setTimeout不可用。如何异步运行函数 查看页面上的多线程脚本执行示例。基本上,JavaScript不直接支持线程,但您可以使用Java线程来实现所需的功能。您可以使用并滚动自己的设置/清除超时和设置/清除间隔函数: var setTimeout, clearTimeout, setInterval, clearInterval; (function () { var timer = new ja

它是一个服务器端Javascript(rhino引擎),因此setTimeout不可用。如何异步运行函数

查看页面上的
多线程脚本执行
示例。基本上,JavaScript不直接支持线程,但您可以使用Java线程来实现所需的功能。

您可以使用并滚动自己的设置/清除超时和设置/清除间隔函数:

var setTimeout,
    clearTimeout,
    setInterval,
    clearInterval;

(function () {
    var timer = new java.util.Timer();
    var counter = 1; 
    var ids = {};

    setTimeout = function (fn,delay) {
        var id = counter++;
        ids[id] = new JavaAdapter(java.util.TimerTask,{run: fn});
        timer.schedule(ids[id],delay);
        return id;
    }

    clearTimeout = function (id) {
        ids[id].cancel();
        timer.purge();
        delete ids[id];
    }

    setInterval = function (fn,delay) {
        var id = counter++; 
        ids[id] = new JavaAdapter(java.util.TimerTask,{run: fn});
        timer.schedule(ids[id],delay,delay);
        return id;
    }

    clearInterval = clearTimeout;

})()
另一个版本使用Rhino 1.7R4,与Rhino 1.7R4兼容,由以下机构提出:


参考资料:

通过您的代码片段,我可以在Rhino中运行Jasmine测试,而无需使用EnvJS。谢谢我希望我能给你100票,非常棒。非常感谢,太棒了。谢谢为了与浏览器完全兼容,您还需要处理延迟的省略。MDN表示,根据HTML5规范,最小延迟是4ms,因此添加以下内容:if(delay==null){delay=4;}NOTA:Rhino 1.7R4有一个bug,它阻止了这个答案中的代码运行。降级至1.7R3或使用更新版本。(查看此讨论了解更新版本的详细信息:)@Alex,我创建的版本使用了
ScheduledThreadPoolExecutor
而不是
Timer
,并在1.7R4中工作:
var setTimeout, clearTimeout, setInterval, clearInterval;

(function () {
    var executor = new java.util.concurrent.Executors.newScheduledThreadPool(1);
    var counter = 1;
    var ids = {};

    setTimeout = function (fn,delay) {
        var id = counter++;
        var runnable = new JavaAdapter(java.lang.Runnable, {run: fn});
        ids[id] = executor.schedule(runnable, delay, 
            java.util.concurrent.TimeUnit.MILLISECONDS);
        return id;
    }

    clearTimeout = function (id) {
        ids[id].cancel(false);
        executor.purge();
        delete ids[id];
    }

    setInterval = function (fn,delay) {
        var id = counter++;
        var runnable = new JavaAdapter(java.lang.Runnable, {run: fn});
        ids[id] = executor.scheduleAtFixedRate(runnable, delay, delay, 
            java.util.concurrent.TimeUnit.MILLISECONDS);
        return id;
    }

    clearInterval = clearTimeout;

})()