function sleep(timeNum) { window.setTimeout(writeNum,1000,timeNum); num=num+parseInt(1);//这句没用,去掉! } </script> TO meizz 我这样做是没有阻止主线程 期待xmlHttp的新版本
ttyp的方法刚查了下 oServerXMLHTTPRequest.setTimeouts(resolveTimeout, connectTimeout, sendTimeout, receiveTimeout); Parameters resolveTimeout A long integer. The value is applied to mapping host names (such as "www.microsoft.com") to IP addresses; the default value is infinite, meaning no timeout.connectTimeout A long integer. The value is applied to establishing a communication socket with the target server, with a default timeout value of 60 seconds.sendTimeout A long integer. The value applies to sending an individual packet of request data (if any) on the communication socket to the target server. A large request sent to a server will normally be broken up into multiple packets; the send timeout applies to sending each packet individually. The default value is 30 seconds.receiveTimeout A long integer. The value applies to receiving a packet of response data from the target server. Large responses will be broken up into multiple packets; the receive timeout applies to fetching each packet of data off the socket. The default value is 30 seconds.
sleep(3);
后面加个
alert("ff");
这个延时就没有了;还望大家帮忙解释一下,对这些东西俺不懂~~~
setTimeout模拟比较不错。
var flag=""
function sleepI(n)
{
flag = window.setInterval("c()",1000*n);
}
function c(){alert("s11");window.clearInterval(flag);} function test()
{
sleepI(3);
alert("s22");
}
</script>
<body>
<button onClick="test();" name="btn" value="BUTTON"></button>
</body>不好意思,我是在简单问题复杂化~~~
browser中的js几乎无法实现真正的sleep,至少我研究到现在没有什么解决方案。只有模态窗口(其实就跟alert一样),能做到非busy的等待,但也无法绕过js单线程的障碍。类似的方式还有在具有java的环境下,调用Thread.sleep,这个等待一点不耗cpu,然而因为无法使用wait,notify,所以还是白搭。如果仅仅满足于非忙等,则上述方案都是可行的。除此之外还有一个技巧,一定程度上可能可以使用非忙等并且同步方式,待我全面验证之后再行发表。
什么叫模拟单线程?你是不是模拟多线程啊?
<head>
<title>emu -- 用command模式模拟多线程</title>
</head>
<body>
<SCRIPT LANGUAGE="JavaScript">
<!--
var commandList = [];
function executeCommands(){
if (commandList.length>0){
commandList.shift()(); }
}function startNewTask(){
var resultTemp = document.createElement("span");
document.body.insertBefore(resultTemp,document.body.lastChild);
document.body.insertBefore(document.createElement("br"),document.body.lastChild);
resultTemp.innerText = 0;
commandList.push(function(){simThread(resultTemp,0);});
}function simThread(temp,n){
temp.innerText = temp.innerText-(-n);
if (n<1000)
commandList.push(function(){simThread(temp,++n);});
else{
document.body.removeChild(temp.nextSibling);
document.body.removeChild(temp);
}
}window.onload = function(){setInterval("executeCommands()",1);}//-->
</SCRIPT>
<button onclick="startNewTask()">start</button><BR><BR>
</body>
</html>
import java.io.PrintWriter;import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;/**
* Servlet implementation class for Servlet: HttpStreamServlet
*
*/
public class HttpStreamServlet extends javax.servlet.http.HttpServlet implements javax.servlet.Servlet {
/**
*
*/
private static final long serialVersionUID = 1L; /* (non-Java-doc)
* @see javax.servlet.http.HttpServlet#HttpServlet()
*/
public HttpStreamServlet() {
super();
}
/* (non-Java-doc)
* @see javax.servlet.http.HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
PrintWriter out = response.getWriter();
int time = Integer.parseInt(request.getParameter("time"));
try {
Thread.sleep(time);
out.println(time);
} catch (InterruptedException e) {
throw new ServletException(e);
} }
/* (non-Java-doc)
* @see javax.servlet.http.HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
}
}
以上,如果以 sleep?time=2000 调用(假设该servlet被mapping到/sleep上),则会等待2秒后输出2000。这个方法适用于所有具有同步调用的环境,不过缺点是,时间不太准确,因为要访问server,虽然可以进一步引入时间同步代码,但总不适于要求等待时间较短的情况。唉,如果xmlhttprequest能设timeout就好了。
function sleep(n)
{
var start=new Date().getTime();
while(true) if(new Date().getTime()-start>n) break;
}
for(var i = 1;i<11;i++){
document.write("第" + i + "行<br>");
sleep(1000);
}
</script>
用你的方法测试了一下, 结果要等待10秒全才全部一次输出来
等会我去试试
代码如下:<html><head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>JK:支持民族工业,尽量少买X货</title><script language=javascript>function sleep(num) //JK
{
var tempDate=new Date();
var theXmlHttp = new ActiveXObject( "Microsoft.XMLHTTP" );
while((new Date()-tempDate)<num )
{
try{
theXmlHttp .open( "get", "http://www.google.com/JK.asp?JK="+Math.random(), false );
theXmlHttp .send();
}
catch(e){;}
}
return;
}
</script>
</head><body>
js不耗(耗得比较少)cpu的
sleep(ms):<input value=10000 name=aaa > ,<input type=button value="alert(0);sleep(..);alert(1);" onclick="alert(0);sleep(document.all.aaa.value);alert(1);">
</body></html>
<html><head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>JK:支持民族工业,尽量少买X货</title><script language=javascript>function sleep(num) //JK
{
var tempDate=new Date();
var tempStr="";
var theXmlHttp = new ActiveXObject( "Microsoft.XMLHTTP" );
while((new Date()-tempDate)<num )
{
tempStr+="\n"+(new Date()-tempDate);
try{
theXmlHttp .open( "get", "about:blank?JK="+Math.random(), false );
theXmlHttp .send();
}
catch(e){;}
}
containerDiv.innerText=tempStr;
return;
}
</script>
</head><body>
js不耗(耗得比较少)cpu的
sleep(ms):<input value=10000 name=aaa > ,<input type=button value="alert(0);sleep(..);alert(1);" onclick="alert(0);sleep(document.all.aaa.value);alert(1);"> <div id=containerDiv>
</div>
</body></html>
这条很耗cpu的!
定时执行都可以用setTimeout来解决
虽说比较耗cpu,但不至于多得成为耗cpu的主要原因
tempStr只是为了演示url与cpu耗用以及时间精度的关系
改一下url可以看到不同的cup耗用和不同的时间精度
如果把url定成google,好像有点对不起google。
如果把url定成一个不存在的url,精度就相当于变成request的timeout时间
如果把url定成about:blank,那这样只是耗掉自己的cup
既然传对象参数可以重载setTimeout函数,为什么不重载这个函数的第二个参数
把第二个参数调大,已达到sleep的目的。
可惜的是没有。另,to:hbhbhbhbhb1021(天外水火(我要多努力))
window.setTimeout方法是立即返回,
sleep应该是希望过一段时间再返回吧.我也不觉得sleep有什么用处
编码这么多年,还没碰到过要用的情况呢
但是IE浏览器好像不会释放递归里面变量的内存。
只要一两次运行的话,既然javascript没有多线程的设计,就用浏览器自己的刷新机制来运行吧。
var lResolve = 5 * 1000;
var lConnect = 5 * 1000;
var lSend = 15 * 1000;
var lReceive = 15 * 1000;
xmlServerHttp.setTimeouts(lResolve, lConnect, lSend, lReceive);
xmlServerHttp.open("GET", "http://localhost/sample.xml", false);
xmlServerHttp.send();
setTimeout是立即返回,是不能完全实现sleep那种,只是模拟,有点象。执行的时候不是有个延迟吗?动态的把延迟加大,也就是说每次执行的延迟时间不同。这样如果是在循环执行过程中的,就可以达到sleep的这个效果,比如
<html><head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>WaterFire</title>
</head>
<script language=javascript>
num=1;
var _st = window.setTimeout;
window.setTimeout = function(fRef,mDelay)
{
if (typeof fRef=="function")
{
if(arguments.length>2)
{
mDelay=parseInt(arguments[2])*parseInt(1000)+parseInt(mDelay);
return _st(fRef,mDelay)
}
}
return _st(fRef,mDelay);
}
function writeNum()
{
if(num==5)//执行第5次的时候调用sleep方法,延迟5秒执行
{
document.getElementById("div1").innerText=num+"这次延迟";
sleep(5);
}
else
{
document.getElementById("div1").innerText=num;
num=parseInt(num)+parseInt(1);
window.setTimeout("writeNum()",1000);
}
}
function sleep(timeNum)
{
window.setTimeout(writeNum,1000,timeNum);
num=num+parseInt(1);
}
</script>
<body onload=writeNum()>
<div id="div1">
</div>
</body>
</html>
{
window.setTimeout(writeNum,1000,timeNum);
num=num+parseInt(1);//这句没用,去掉!
}
</script>
TO meizz
我这样做是没有阻止主线程
期待xmlHttp的新版本
oServerXMLHTTPRequest.setTimeouts(resolveTimeout, connectTimeout,
sendTimeout, receiveTimeout);
Parameters
resolveTimeout
A long integer. The value is applied to mapping host names (such as "www.microsoft.com") to IP addresses; the default value is infinite, meaning no timeout.connectTimeout
A long integer. The value is applied to establishing a communication socket with the target server, with a default timeout value of 60 seconds.sendTimeout
A long integer. The value applies to sending an individual packet of request data (if any) on the communication socket to the target server. A large request sent to a server will normally be broken up into multiple packets; the send timeout applies to sending each packet individually. The default value is 30 seconds.receiveTimeout
A long integer. The value applies to receiving a packet of response data from the target server. Large responses will be broken up into multiple packets; the receive timeout applies to fetching each packet of data off the socket. The default value is 30 seconds.