setTimeout【注:out的'o'是小写】

如果setTimeout(function,mill)中的function带参数,则定义的毫秒树mill会失效,function方法会立即执行

解决方法:把参数定义为"全局变量"进行操作,或者不要单独写一个方法,直接再同一个方法内直接写function函数,例如本例中的第一个setTimeout;

例子1:

varoperations=[Objectobject,Objectobject,Objectobject,Objectobject,Objectobject];functionsessionPlayback(){playControl(0);}functionplayControl(i){varoperation=operations[i];varcontent=$("#operation_content").val();content+="#>"+operation.request;$("#operation_content").val(content);setTimeout(function(){varcontent=$("#operation_content").val();content+=operation.response+"\r\n";$("#operation_content").val(content);},eval((operation.endTime-operation.startTime)*playSpeed));i++;if(i<operations.length){varjiangetime=0;jiangetime=(operations[i].startTime-operations[i-1].startTime)*playSpeed;console.log("jiangetime:"+jiangetime);setTimeout("playControl("+i+")",jiangetime);}}


例子2:

functionplayControl(){console.log("operations:"+operations);$("#operation_content").val("");for(vari=0;i<operations.length;i++){varjiangetime=0;if(i>0){jiangetime=operations[i].startTime-operations[0].startTime;}console.log("jiangetime:"+jiangetime);setTimeout("showOperationHistory(operations["+i+"])",jiangetime);}$("#operation_content").focus();}functionshowOperationHistory(operation){varcontent=$("#operation_content").val();content+="#>"+operation.request+"\r\n";//加换行console.log("--------------------");$("#operation_content").val(content);setTimeout(function(){varcontent=$("#operation_content").val();content+=operation.response+"\r\n";$("#operation_content").val(content);},eval(operation.endTime-operation.startTime));}


传递的参数尽量是字符串!