转自: http://www.blogjava.net/emu/archive/2005/06/20/6365.html
<html><head><title>emu -- 用fason的参数化定时器模拟多线程</title></head><body> <SCRIPT LANGUAGE="JavaScript"> <!-- var _st = window.setTimeout; window.setTimeout = function(fRef, mDelay) { if(typeof fRef == 'function'){ var argu = Array.prototype.slice.call(arguments,2); var f = (function(){ fRef.apply(null, argu); }); return _st(f, mDelay); } return _st(fRef,mDelay); } var _int = window.setInterval; window.setInterval = function(fRef, mDelay) { if(typeof fRef == 'function'){ var argu = Array.prototype.slice.call(arguments,2); var f = (function(){ fRef.apply(null, argu); }); return _int(f, mDelay); } return _st(fRef,mDelay); } function startNewTask(){ var target = document.getElementById("sampleResult").cloneNode(true); with (target){ id="";style.display="block";style.color=(Math.floor(Math.random()* (1<<23)).toString(16)+"00000").substring(0,6); } document.body.insertBefore(target,document.body.lastChild); var parameter = {target:target,n:0,result:0} parameter.timer = setInterval(count,1,parameter); } function count(parameter){ with (parameter){ if (!target.stop){ for(var i=0;i<speed;i++) if (n<MAX) result += ++n; target.innerHTML = result; } if (n>=MAX){ clearInterval(timer); setTimeout(function(elm){document.body.removeChild(elm)},2000,target); } } } var speed = 1111; var MAX=100000; //--> </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> 点击这里查看效果
1
<
html
><
head
><
title
>
emu -- 用fason的参数化定时器模拟多线程
</
title
></
head
><
body
>
2 < SCRIPT LANGUAGE ="JavaScript" >
3 <!--
4
5 var _st = window.setTimeout;
6 window.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 >
2 < SCRIPT LANGUAGE ="JavaScript" >
3 <!--
4
5 var _st = window.setTimeout;
6 window.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 >
<html><head><title>emu -- 用fason的参数化定时器模拟多线程</title></head><body> <SCRIPT LANGUAGE="JavaScript"> <!-- var _st = window.setTimeout; window.setTimeout = function(fRef, mDelay) { if(typeof fRef == 'function'){ var argu = Array.prototype.slice.call(arguments,2); var f = (function(){ fRef.apply(null, argu); }); return _st(f, mDelay); } return _st(fRef,mDelay); } var _int = window.setInterval; window.setInterval = function(fRef, mDelay) { if(typeof fRef == 'function'){ var argu = Array.prototype.slice.call(arguments,2); var f = (function(){ fRef.apply(null, argu); }); return _int(f, mDelay); } return _st(fRef,mDelay); } function startNewTask(){ var target = document.getElementById("sampleResult").cloneNode(true); with (target){ id="";style.display="block";style.color=(Math.floor(Math.random()* (1<<23)).toString(16)+"00000").substring(0,6); } document.body.insertBefore(target,document.body.lastChild); var parameter = {target:target,n:0,result:0} parameter.timer = setInterval(count,1,parameter); } function count(parameter){ with (parameter){ if (!target.stop){ for(var i=0;i<speed;i++) if (n<MAX) result += ++n; target.innerHTML = result; } if (n>=MAX){ clearInterval(timer); setTimeout(function(elm){document.body.removeChild(elm)},2000,target); } } } var speed = 1111; var MAX=100000; //--> </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> 点击这里查看效果