日期:2014-05-16  浏览次数:20628 次

为何这里的时间戳要定义为0呢?
就这个教程: http://www.jb51.net/article/12977_3.htm



<html> 
<head> 
<title>Ajax with jQuery Example</title> 
<script type="text/JavaScript" src="jquery.js"></script> 
<script type="text/JavaScript"> 
$(document).ready(function(){ 
timestamp = 0; 
updateMsg(); 
$("form#chatform").submit(function(){ 
$.post("backend.php",{ 
message: $("#msg").val(), 
name: $("#author").val(), 
action: "postmsg", 
time: timestamp 
}, function(xml) { 
$("#msg").empty(); 
addMessages(xml); 
}); 
return false; 
}); 
}); 
function addMessages(xml) { 
if($("status",xml).text() == "2") return; 
timestamp = $("time",xml).text(); 
$("message",xml).each(function(id) { 
message = $("message",xml).get(id); 
$("#messagewindow").prepend("<b>"+$("author",message).text()+ 
"</b>: "+$("text",message).text()+ 
"<br />"); 
}); 

function updateMsg() { 
$.post("backend.php",{ time: timestamp }, function(xml) { 
$("#loading").remove(); 
addMessages(xml); 
}); 
setTimeout('updateMsg()', 4000); 

</script> 
<style type="text/css"> 
#messagewindow { 
height: 250px; 
border: 1px solid; 
padding: 5px; 
overflow: auto; 

#wrapper { 
margin: auto; 
width: 438px; 

</style> 
</head> 
<body> 
<div id="wrapper"> 
<p id="messagewindow"><span id="loading">Loading...</span></p> 
<form id="chatform"> 
Name: <input type="text" id="author" /> 
Message: <input type="text" id="msg" /> 
<input type="submit" value="ok" /><br /> 
</form> 
</div> 
</body> 
</html> 



其中timestamp = 0; 后面也没有再把它进行修改(加1什么的),为何要这样呢?时间戳不是为了保证ajax请求的唯一性么?但这个示例里貌似都是0。我总是认为应该是timestamp =getTime();合适点吧?但连我书上的教程也是设置timestamp = 0,真心郁闷,求大神指导下!!!感谢!!!

------解决方案--------------------
本帖最后由 showbo 于 2013-03-29 16:50:54 编辑
post不存在缓存的问题,设置为什么都可以

如果是get请求,那么你比写教程的人聪明
------解决方案--------------------
对post来说是多余的。。

get的话一定要new Date().getTime(),要不IE下会有缓存问题