为什么要用interval不用setTimeout两者功能差不多 但setTimeout可以阻塞
开始timeout-->到时间运行function-->function结束-->阻塞函数-->再调用timeout-->循环

解决方案 »

  1.   

    interval只是间隔,下面的例子很容易看出来他始终是一条线程function aa()
    {
    var ss = document.createElement("div");
    document.body.appendChild(ss);
    for(var i=0;i<2000;i++)ss.innerHTML = i;
    }
    var ee = setInterval(aa,10);
    document.onmousedown = function(){clearInterval(ee);}但如果加点手脚
    var ee = setInterval(setTimeout(aa,2000),10);
    这样当你按下鼠标停止interval的时候仍然会继续执行几次 这表示已经开始了多条线程
    至于后面如何应用就看你自己的了
      

  2.   

    不好意思改成
    var ee = setInterval(function(){setTimeout(aa,2000)},10);
      

  3.   

    但如果加点手脚
    var ee = setInterval(setTimeout(aa,2000),10);
    这样当你按下鼠标停止interval的时候仍然会继续执行几次 这表示已经开始了多条线程
    至于后面如何应用就看你自己的了回gzdiablo() :
    那主要是因为有个timeout,而不是多线程吧?据我粗浅的认识,异步的回调才可能是多线程?
      

  4.   

    回gzdiablo() :插了几个桩试试:var count = new Object;
    count.n = 0;
    var actions = new Array;
    function aa(count)
    {
      actions.push('start ' + count.n);
      var ss = document.createElement("div");
      document.body.appendChild(ss);
      for(var i=0;i<2000;i++)
        ss.innerHTML = i;
      actions.push('finish ' + count.n);
      count.n ++;
    }
    var ee = setInterval(function(){setTimeout(aa,200, count);},10);
    document.onmousedown = function(){clearInterval(ee);}插得不是很好,但不妨碍看结果,出来的结果都是start/finish成对出现的,后面的数字也匹配。可见还是单线的
      

  5.   

    JS在IE浏览器中只能模拟多线程,可以和客户协商,把那些步骤分成一些零碎的步骤人为触发来进行下面有一个EMU的模拟代码
    <html><head><title>emu -- 用command模式模拟多线程</title></head><body>
     <SCRIPT LANGUAGE="JavaScript">
     <!--
     if (Array.prototype.shift==null)
     Array.prototype.shift = function (){
         var rs = this[0];
         for (var i=1;i<this.length;i++) this[i-1]=this[i]
         this.length=this.length-1
         return rs;
    }
    if (Array.prototype.push==null)
    Array.prototype.push = function (){
        for (var i=0;i<arguments.length;i++) this[this.length]=arguments[i];
        return this.length;
    }var commandList = [];
    var nAction = 0;//控制每次运行多少个动作
    var functionConstructor = function(){}.constructor;
    function executeCommands(){
        for (var i=0;i<nAction;i++)
            if (commandList.length>0){
                var command = commandList.shift();
                if (command.constructor == functionConstructor)
                    if (command.scheduleTime == null || new Date()-command.scheduleTime>0)
                        command();
                    else
                        commandList.push(command);
            }
    }function startNewTask(){
        var resultTemp = document.getElementById("sampleResult").cloneNode(true);
        with (resultTemp){
        id="";style.display="block";style.color=(Math.floor(Math.random()* (1<<23)).toString(16)+"00000").substring(0,6);
        }
        document.body.insertBefore(resultTemp,document.body.lastChild);
        commandList.push(function(){simThread(resultTemp,1);});
        nAction++;
    }function  simThread(temp,n){
        if (temp.stop) n--;
        else temp.innerHTML = temp.innerHTML - (-n);
        if (n<1000)
            commandList.push(function(){simThread(temp,++n)});
        else{
            var command = function(){document.body.removeChild(temp);;nAction--;};
            command.scheduleTime = new Date()-(-2000);
            commandList.push(command);
        }
    }window.onload = function(){setInterval("executeCommands()",1);}
    //-->
    </SCRIPT>
    <button onclick="startNewTask()">开始新线程</button><BR><BR>
    <div id=sampleResult onmouseover="this.stop=true" onmouseout="this.stop=false" style="display:none;cursor:hand">0</div>
    </body>
    </html>
      

  6.   

    - -
    if (Array.prototype.shift==null)
      

  7.   

    刚才用监控软件看了一下 的确是只有单线程 
    我之前写了一个树,因为使用settimeout导致 节点位置都乱了和多线程很类似 所以一直以为是开启了多线程 导致理解错误
      

  8.   

    to hbhbhbhbhb1021(天外水火(我要多努力)):看了一下代码,原理就在1ms的interval中接受用户的onclick事件?
      

  9.   

    而且,这个东西跑得很爽个人认为是因为这样的加减法确实耗不了多少时间。遇上new一个类,执行一段时间的代码,这样那个executeCommands 里的循环就被阻塞了,对么?
      

  10.   

    我的意思是这个command太简单了,所以执行时间太短,看不大出来而已。整个过程就是:把线程故意留一段时间出来(interval),用于维护这个CommandList,阻塞完后再执行command?但奇怪的是我在startNewTask()的最后一行加了一个alert('dd');function startNewTask(){
        var resultTemp = document.getElementById("sampleResult").cloneNode(true);
        with (resultTemp){
        id="";style.display="block";style.color=(Math.floor(Math.random()* (1<<23)).toString(16)+"00000").substring(0,6);
        }
        document.body.insertBefore(resultTemp,document.body.lastChild);
        commandList.push(function(){simThread(resultTemp,1);});
        nAction++;
        alert('dd');
    }按说,线程应该阻塞了?
    但这个数字仍然在变。我试着把interval设为1000,alert仍然没有阻塞,说明是两个线程?谁来解释一下?
      

  11.   

    to zhaoxiaoyang(梅雪香@深圳):需求都在一楼写了。举个例子,本来我只用干三件事,定好了怎么干并已经开始干了,客户的干涉开始了,而我需要更新这些任务。
      

  12.   

    to zhaoxiaoyang(梅雪香@深圳):看您两颗星了,摆脱帮忙看看吧,呵呵。
      

  13.   

    没人解释了么?1. 异步回调是不是多线程?
    2. 先前贴子里那个alert阻塞了哪个线程?
      

  14.   

    如果你要用多线程,模拟得真实一些的话,可以试试用iframe来实现
      

  15.   

    :D
    iframe果然不错。不过IE的alert很BT呢。。<iframe style="display:none;"></iframe>
    <script type="text/javascript">
    window.onload = function () {
    var frame = window.frames[0], i = 0;
    var func = frame.window.func = function () {
    document.getElementById("status").innerHTML = i ++;
    frame.window.setTimeout(func, 1000);
    }
    frame.window.func();
    window.setTimeout("alert(1)", 5000);
    };
    </script>
    <div id="status"></div>
      

  16.   

    FF测试的话。。不会停止。。IE则停止。。
      

  17.   

    还有。。
    IE的this指的貌似有点问题
    frame.window.setTimeout(this.func, 10);在IE测试this哪也没指貌似。。在FF测试this指的就是frame.window;
      

  18.   

    to muxrwc(十月,改变) :FF测试的话。。不会停止。。IE则停止。。这点谁能解释一下?this理论应该是指向frame.window,IE
      

  19.   

    to BlueDestiny(Eventually never-online) :Iframe很好
    用Iframe处理可以并行的发一些请求,但本身自己的复杂性至少要上升一个数量级?
    包括sibling间的交互.top-iframes间的交互。现在一个场景:
    iframe中的回调在更改top.window或兄弟iframe中的一个数组,而这个数组正在迭代,这时候是怎样的顺序处理呢?
      

  20.   

    还有刚才的问题,依然没人解答:1. 异步回调是不是多线程?
    2. 先前贴子里那个alert阻塞了哪个线程?各位请费神看看。我觉得对大家搞清楚这些问题都有些帮助的,不是么?
      

  21.   

    LZ你先看下。。http://community.csdn.net/Expert/topic/5448/5448164.xml?temp=.5400507
      

  22.   

    虽然和我发的那个执行iframe的setTimeout不同不过FF的alert和IE的不同
    对于
    2. 先前贴子里那个alert阻塞了哪个线程? 问题。。应该已经是答案了吧。。对于XMLHTTP貌似可以实现多线程的效果不过没具体测试过
    因为它可以实现一次弹出多个alert(在IE里)
    http://www.zhb.org.cn/wc/ajax/
    这个是xmlHTTP的测试。。IE是若干个alert,FF则是一个下面的code是测试iframe的IE和FF差别结果。。<iframe style="display:none;"></iframe>
    <script type="text/javascript">
    window.onload = function () {
    var frame = window.frames[0], i = 0;
    var func = frame.window.func = function () {
    document.getElementById("status").innerHTML = i ++;
    frame.window.setTimeout(func, 1000); //这个IFRAME的ALERT不会阻止
    //另外iframe里还可以这样写IE则不行
    //*****this.setTimeout(this.func, 1000)

    //window.setTimeout(func, 1000); //这个则会阻止
    }
    frame.window.func();
    window.setTimeout("alert(1)", 5000);
    };
    </script>
    <div id="status"></div>
      

  23.   

    to muxrwc(十月,改变) :http://community.csdn.net/Expert/topic/5448/5448164.xml?temp=.5400507刚试了下
    FF在这种情况下,alert并不是不阻塞。
    而是阻塞很短一段时间。也就是说alert本身是和函数在“抢”时间干活
      

  24.   

    to muxrwc(十月,改变) :你的例子可能有点问题
      

  25.   


    1. 异步回调是不是多线程?
    2. 先前贴子里那个alert阻塞了哪个线程?首先,一个page在脚本运行只有一个thread,如果能够调用activeX做的muti-thread,那是另外一回事。应该说这两个问题都是不属于脚本方面的事了
    因为如果你用XMLHTTP的话,XMLHTTP的异步是ActiveX(ie7.0以下版本)管的事,activeX拥有多线程的性质。ActiveX本身的性质发出的request,属脚本不可控制范围(除了几个API)。但如果回到脚本之后,仍然是单线程。题外的一些话:alert在IE里和mozilla里都不一样的,但它们的参考里都是这个意思:
    Displays a dialog box containing an application-defined message。
    但mozilla和IE的kernel不同,这得看具体情况而定about:
    Iframe很好
    用Iframe处理可以并行的发一些请求,但本身自己的复杂性至少要上升一个数量级?
    包括sibling间的交互.top-iframes间的交互。这个要看怎么处理了,这中间多了一个交互的代码,关键看怎么处理和控制iframe。封装得好的话应该问题不大,把iframe看作是一个thread对象,用一个object来管理,用一个array当作线程池。
      

  26.   

    to BlueDestiny(Eventually never-online) :但如果回到脚本之后,仍然是单线程。
    ---------------------------------------------------还是这个场景:
    iframe中的回调在更改top.window或兄弟iframe中的一个数组,而这个数组正在迭代,这时候是怎样的顺序处理呢?还是两个线程同时干活?如果这样的话是不是处理的时候还要给这个数组加锁以保持数据的一致性?
      

  27.   

    to BlueDestiny(Eventually never-online) :
    因为如果你用XMLHTTP的话,XMLHTTP的异步是ActiveX(ie7.0以下版本)管的事,activeX拥有多线程的性质。ActiveX本身的性质发出的request,属脚本不可控制范围(除了几个API)。但如果回到脚本之后,仍然是单线程。
    -----------------------------------------------------
    事例:http://www.zhb.org.cn/wc/ajax/那IE在这里的两个alert怎么解释呢?PS:看样子 FF 想用XMLHTTP的异步请求来玩多线程是不可能了?但这个例子中又牵涉到了alert的机制,还下不了定论