【原创】 实现Javascript的 sleep(ms) 实现暂停,可用于服务器
朋友问在Javascript中如何实现Sleep,我知道原来没有,但我想,也许可以通过微软的COM来实现。
我第一想到了 XMLHttp,下面是我的实现方案。希望起到抛砖引玉的作用。
因为:Msxml2.ServerXMLHTTP 只能在本地或服务器脚本中运行。
点下面的 “运行代码” 按钮将没有权限执行。所在应该把下面的代理复制,新建一个Html文件,来运行。
1
<html>
2
<head>
3
<title>阿牛测试</title>
4
</head>
5
<body>
6
<script language=javascript>
7
/*
8
* 函数名: evlon.sleep(ms)
9
* 作者: evlon(阿牛) MSN:niukl@msn.com QQ:273352165
10
* 功能: 模拟 C 语言的 Sleep
11
* 用途: 用到时就有用了,最简单的情况就是在聊天室的服务端中,
12
* 通过循环把Application的内容写到客户端,但这样会CPU占用率过高
13
* 这时把这个函数加入,会几乎不占用CPU
14
*
15
*/
16
function Evlon()
17
{
18
this.xh = new ActiveXObject("Msxml2.ServerXMLHTTP");
19
this.lresolveTimeout = 0; // 解析DNS名字的超时时间
20
this.lconnectTimeout = 500; // 建立Winsock连接的超时时间
21
this.lsendTimeout = 0 ; // 发送数据的超时时间
22
this.lreceiveTimeout = 0 ; // 接收response的超时时间
23
this.xh.setTimeouts(this.lresolveTimeout,this.lconnectTimeout,this.lsendTimeout,this.lreceiveTimeout);
24
this.urlport = "http://127.0.0.1:1111";
25![](/Images/OutliningIndicators/InBlock.gif)
26
}
27
Evlon.prototype.sleep = function(ms)
28
{
29
var beg = new Date();
30
31
var count = Math.floor(ms / 500);
32
var lastms = ms % 500;
33
this.xh.setTimeouts(this.lresolveTimeout,this.lconnectTimeout,this.lsendTimeout,this.lreceiveTimeout);
34
35
for(var i = 0 ;i< count + 1; ++i)
36
{
37
//先看一下时间过了没有
38
var now = new Date();
39
if(ms < now - beg)
40
{
41
break;
42
}
43
else
44
{
45
if( i == count)
46
{
47
this.xh.setTimeouts(this.lresolveTimeout,lastms,this.lsendTimeout,this.lreceiveTimeout);
48
}
49
50
//Sleep
51
try
52
{
53
this.xh.open("GET",this.urlport,false,null,null);
54
this.xh.send();
55
}
56
catch(e){
57
}
58
59
}
60
}
61
62
}
63
64
65
66
//下面是测试程序,在指定时间内观查CPU占用率
67
function testSleep(timeSpan /*秒*/)
68
{
69
try
70
{
71
var beg = new Date();
72
var evlon = new Evlon();
73
var nCount = 0;
74
while(true)
75
{
76
var end = new Date();
77
var span = end - beg;
78
79
if(span > timeSpan * 1000)
80
{
81
break;
82
}
83
84
nCount ++;
85
evlon.sleep(200);
86
}
87
88
var end = new Date();
89
var span = end - beg;
90
var msg = '共运行' + nCount.toString() + '次\n占用时间' + span + '毫秒';
91
alert(msg);
92
}
93
catch(e)
94
{
95
alert(e.message);
96
}
97
}
98
99
testSleep(5);
100
101
</script>
102
</body>
103
</html>
![](/Images/OutliningIndicators/None.gif)
2
![](/Images/OutliningIndicators/None.gif)
3
![](/Images/OutliningIndicators/None.gif)
4
![](/Images/OutliningIndicators/None.gif)
5
![](/Images/OutliningIndicators/None.gif)
6
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
7
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
8
![](/Images/OutliningIndicators/InBlock.gif)
9
![](/Images/OutliningIndicators/InBlock.gif)
10
![](/Images/OutliningIndicators/InBlock.gif)
11
![](/Images/OutliningIndicators/InBlock.gif)
12
![](/Images/OutliningIndicators/InBlock.gif)
13
![](/Images/OutliningIndicators/InBlock.gif)
14
![](/Images/OutliningIndicators/InBlock.gif)
15
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
16
![](/Images/OutliningIndicators/InBlock.gif)
17
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
18
![](/Images/OutliningIndicators/InBlock.gif)
19
![](/Images/OutliningIndicators/InBlock.gif)
20
![](/Images/OutliningIndicators/InBlock.gif)
21
![](/Images/OutliningIndicators/InBlock.gif)
22
![](/Images/OutliningIndicators/InBlock.gif)
23
![](/Images/OutliningIndicators/InBlock.gif)
24
![](/Images/OutliningIndicators/InBlock.gif)
25
![](/Images/OutliningIndicators/InBlock.gif)
26
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
27
![](/Images/OutliningIndicators/InBlock.gif)
28
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
29
![](/Images/OutliningIndicators/InBlock.gif)
30
![](/Images/OutliningIndicators/InBlock.gif)
31
![](/Images/OutliningIndicators/InBlock.gif)
32
![](/Images/OutliningIndicators/InBlock.gif)
33
![](/Images/OutliningIndicators/InBlock.gif)
34
![](/Images/OutliningIndicators/InBlock.gif)
35
![](/Images/OutliningIndicators/InBlock.gif)
36
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
37
![](/Images/OutliningIndicators/InBlock.gif)
38
![](/Images/OutliningIndicators/InBlock.gif)
39
![](/Images/OutliningIndicators/InBlock.gif)
40
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
41
![](/Images/OutliningIndicators/InBlock.gif)
42
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
43
![](/Images/OutliningIndicators/InBlock.gif)
44
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
45
![](/Images/OutliningIndicators/InBlock.gif)
46
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
47
![](/Images/OutliningIndicators/InBlock.gif)
48
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
49
![](/Images/OutliningIndicators/InBlock.gif)
50
![](/Images/OutliningIndicators/InBlock.gif)
51
![](/Images/OutliningIndicators/InBlock.gif)
52
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
53
![](/Images/OutliningIndicators/InBlock.gif)
54
![](/Images/OutliningIndicators/InBlock.gif)
55
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
56
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
57
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
58
![](/Images/OutliningIndicators/InBlock.gif)
59
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
60
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
61
![](/Images/OutliningIndicators/InBlock.gif)
62
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
63
![](/Images/OutliningIndicators/InBlock.gif)
64
![](/Images/OutliningIndicators/InBlock.gif)
65
![](/Images/OutliningIndicators/InBlock.gif)
66
![](/Images/OutliningIndicators/InBlock.gif)
67
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
68
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
69
![](/Images/OutliningIndicators/InBlock.gif)
70
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
71
![](/Images/OutliningIndicators/InBlock.gif)
72
![](/Images/OutliningIndicators/InBlock.gif)
73
![](/Images/OutliningIndicators/InBlock.gif)
74
![](/Images/OutliningIndicators/InBlock.gif)
75
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
76
![](/Images/OutliningIndicators/InBlock.gif)
77
![](/Images/OutliningIndicators/InBlock.gif)
78
![](/Images/OutliningIndicators/InBlock.gif)
79
![](/Images/OutliningIndicators/InBlock.gif)
80
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
81
![](/Images/OutliningIndicators/InBlock.gif)
82
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
83
![](/Images/OutliningIndicators/InBlock.gif)
84
![](/Images/OutliningIndicators/InBlock.gif)
85
![](/Images/OutliningIndicators/InBlock.gif)
86
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
87
![](/Images/OutliningIndicators/InBlock.gif)
88
![](/Images/OutliningIndicators/InBlock.gif)
89
![](/Images/OutliningIndicators/InBlock.gif)
90
![](/Images/OutliningIndicators/InBlock.gif)
91
![](/Images/OutliningIndicators/InBlock.gif)
92
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
93
![](/Images/OutliningIndicators/InBlock.gif)
94
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
95
![](/Images/OutliningIndicators/InBlock.gif)
96
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
97
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
98
![](/Images/OutliningIndicators/InBlock.gif)
99
![](/Images/OutliningIndicators/InBlock.gif)
100
![](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
101
![](/Images/OutliningIndicators/None.gif)
102
![](/Images/OutliningIndicators/None.gif)
103
![](/Images/OutliningIndicators/None.gif)
QQ:273352165
evlon#126.com
转载请注明出处。