setTimeout【注:out的‘o‘是小写】
如果setTimeout(function,mill)中的function带参数,则定义的毫秒树mill会失效,function方法会立即执行
解决方法:把参数定义为"全局变量"进行操作,或者不要单独写一个方法,直接再同一个方法内直接写function函数,例如本例中的第一个setTimeout;
例子1:
var operations =[Object object,Object object,Object object,Object object,Object object]; function sessionPlayback(){ playControl(0); } function playControl(i){ var operation = operations[i]; var content = $("#operation_content").val(); content+="#>"+operation.request; $("#operation_content").val(content); setTimeout(function(){ var content = $("#operation_content").val(); content+= operation.response+"\r\n"; $("#operation_content").val(content); },eval((operation.endTime - operation.startTime)*playSpeed)); i ++; if(i<operations.length){ var jiangetime = 0; jiangetime = (operations[i].startTime - operations[i-1].startTime)*playSpeed; console.log("jiangetime:"+jiangetime); setTimeout("playControl("+i+")",jiangetime); } }
例子2:
function playControl(){ console.log("operations:"+operations); $("#operation_content").val(""); for(var i = 0;i < operations.length;i++){ var jiangetime = 0; if(i>0){ jiangetime = operations[i].startTime - operations[0].startTime; } console.log("jiangetime:"+jiangetime); setTimeout("showOperationHistory(operations["+i+"])",jiangetime); } $("#operation_content").focus(); } function showOperationHistory(operation){ var content = $("#operation_content").val(); content+="#>"+operation.request+"\r\n";//加换行 console.log("--------------------"); $("#operation_content").val(content); setTimeout(function(){ var content = $("#operation_content").val(); content+= operation.response+"\r\n"; $("#operation_content").val(content); },eval(operation.endTime - operation.startTime)); }
传递的参数尽量是字符串!
时间: 2024-10-20 21:09:46