emu in blogjava

            BlogJava :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理 ::
            171 隨筆 :: 103 文章 :: 1052 評論 :: 2 Trackbacks
          原帖子發表在 http://community.csdn.net/Expert/TopicView.asp?id=4069144 author:emu(黃希彤)

          有csdn的朋友把http://community.csdn.net/Expert/TopicView.asp?id=3191873 給翻了出來,這樣在javascript中模擬多線程又有了更漂亮的做法了:

           1<html><head><title>emu -- 用fason的參數化定時器模擬多線程</title></head><body>
           2<SCRIPT LANGUAGE="JavaScript">
           3<!--
           4
           5var _st = window.setTimeout;
           6window.setTimeout = function(fRef, mDelay) {
           7    if(typeof fRef == 'function'){
           8        var argu = Array.prototype.slice.call(arguments,2);
           9        var f = (function(){ fRef.apply(null, argu); });
          10        return _st(f, mDelay);
          11    }

          12    return _st(fRef,mDelay);
          13}

          14
          15var _int = window.setInterval;
          16window.setInterval = function(fRef, mDelay) {
          17    if(typeof fRef == 'function'){
          18        var argu = Array.prototype.slice.call(arguments,2);
          19        var f = (function(){ fRef.apply(null, argu); });
          20        return _int(f, mDelay);
          21    }

          22    return _st(fRef,mDelay);
          23}

          24
          25
          26function startNewTask(){
          27    var target = document.getElementById("sampleResult").cloneNode(true);
          28    with (target){
          29    id="";style.display="block";style.color=(Math.floor(Math.random()* (1<<23)).toString(16)+"00000").substring(0,6);
          30    }

          31    document.body.insertBefore(target,document.body.lastChild);
          32    var parameter = {target:target,n:0,result:0}
          33    parameter.timer = setInterval(count,1,parameter);
          34}

          35
          36function  count(parameter){
          37    with (parameter){
          38        if (!target.stop){
          39            for(var i=0;i<speed;i++)
          40                if (n<MAX) result += ++n;
          41            target.innerHTML = result;
          42        }

          43        if (n>=MAX){
          44            clearInterval(timer);
          45            setTimeout(function(elm){document.body.removeChild(elm)},2000,target);
          46        }

          47    }

          48}

          49
          50var speed = 1111;
          51var MAX=100000;
          52//-->
          53
          </SCRIPT>
          54<button onclick="startNewTask()">開始新線程</button>
          55
          56<BR><BR>
          57<div id=sampleResult onmouseover="this.stop=true" onmouseout="this.stop=false" style="display:none;cursor:hand">0</div>
          58</body>
          59</html>


          在IE6和firefox1.5下面通過

          有的時候command模式也許不是最好的辦法,比如我之前寫的例子。寫那個例子純粹只是為了演示command確實可以用在javascript中,并不表示我們任何時候都應該優先考慮這樣做。 至于command模式本身,我仍認為它是最簡潔優美的模式之一,在我們用各種語言解決問題的時候都可以考慮使用它,而不止于j2se。 套一句名言:如果你的工具箱里面只有榔頭這一樣工具,那么每個問題在你的眼里看起來都象釘子author:emu(黃希彤)
          posted on 2005-06-20 10:59 emu 閱讀(2058) 評論(2)  編輯  收藏

          評論

          # re: 用fason的參數化定時器在javascript中模擬多線程 2006-09-19 15:55 goldduck
          不明白,在實際處理中有何意義  回復  更多評論
            

          # re: 用fason的參數化定時器在javascript中模擬多線程 2006-09-19 21:40 emu
          主要是在我們希望進行大量運算又不希望堵塞用戶交互的情況下可以考慮這樣處理。不過就算現在沒有釘子,工具箱里有把榔頭也沒什么不好啊。  回復  更多評論
            


          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          主站蜘蛛池模板: 华安县| 兴海县| 滦南县| 西吉县| 唐山市| 简阳市| 封丘县| 竹北市| 兴仁县| 东港市| 会同县| 建水县| 米脂县| 佛学| 乌拉特前旗| 珠海市| 油尖旺区| 鲜城| 榆中县| 台北市| 泸水县| 奇台县| 疏勒县| 平南县| 嘉禾县| 乐昌市| 黑龙江省| 萝北县| 察隅县| 郧西县| 辽宁省| 宁强县| 巩义市| 宜兰县| 嫩江县| 封开县| 手游| 长丰县| 乐安县| 定日县| 库车县|