碰到需要一个sleep()功能。JS中的有window.setTimeout(https://developer.mozilla.org/En/DOM/Window.setTimeout),
但是是异步执行。Google一下,发现一个有意思的同步XMLHttpRequest的实现。
client端:
<%@ page language="java" contentType="text/html;charset=UTF-8"%>
<html locale="true">
<head>
<script type="text/javascript">
Util = function() {
function getXRequest() {
activeX = ['MSXML2.XMLHTTP.3.0', 'MSXML2.XMLHTTP', 'Microsoft.XMLHTTP']
try {
http = new XMLHttpRequest();
} catch (e) {
for (var i = 0; i < activeX.length; ++i) {
try {
http = new ActiveXObject(activeX[i]);
break;
} catch (e) {
}
}
} finally {
return http;
}
}
return {
sleep : function(sec) {
xreq = getXRequest()
startTime = new Date()
while ((new Date().getTime() - startTime) < sec) {
xreq.open('GET', '/hellowar/sleep.jsp', false);
xreq.send(null);
}
}
}
}()
</script>
</head>
<body bgcolor="white">
<button onclick="javascript:Util.sleep(10000);alert('Delay 10 Sec.')">click me</button>
</body>
</html>
server端:
<%@ page language="java" contentType="text/html;charset=UTF-8"%>
<%
try {
Thread.sleep(10000);
} catch (InterruptedException e) {
e.printStackTrace();
}
%>
<html locale="true">
<head>
</head>
<body bgcolor="white">
dummy content
</body>
</html>
posted @
2008-10-29 22:56 Ether 阅读(6483) |
评论 (0) |
编辑 收藏