Function band Parameters of settimeout

Source: Internet
Author: User

SetTimeout "Note: Out of ' o ' is lowercase"

If the function with parameters in settimeout (Function,mill), the defined millisecond tree mill is invalidated and the function method executes immediately

Workaround: Define the parameter as a "global variable", or do not write a method alone, directly in the same method directly write function functions, such as the first settimeout in this example;

Example 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);}}


Example 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";//Add Line 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));}


Pass the argument as far as the string!


This article is from the "Oriental Pavilion" blog, please be sure to keep this source http://lailai.blog.51cto.com/3362373/1692810

Function band Parameters of settimeout

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.