JS settimeout function

Source: Internet
Author: User

Recently in the Reading JS DOM programming art, in the tenth chapter of the animation there is a settimeout function in the example of a lot of quotes, study a good freshman will only see, comprehensive online interpretation of the great God and their own understanding, the principle is this:

First look at the program source code:

1 functionmoveelement (elementid,final_x,final_y,interval) {2     if(!document.getelementbyid)return false;3     if(!document.getelementbyid (ElementID))return false;4     varElem =document.getElementById (elementid);5     varXpos =parseint (elem.style.left);6     varYpos =parseint (elem.style.top);7     if(Xpos==final_x && ypos==final_y) {8         return true;9     }Ten     if(xpos<final_x) { Onexpos++; A     } -     if(xpos>final_x) { -xpos--; the     } -     if(ypos<final_y) { -ypos++; -     } +     if(ypos>final_y) { -ypos--; +     } AElem.style.left = xpos + "px"; atElem.style.top = ypos + "px"; -     varrepeat = "moveelement ('" +elementid+ "'," +final_x+ "," +final_y+ "," +interval+ ")"; -Movement =setTimeout (repeat,interval); -}
The function of this code is to get the element's position to final_x,final_y at every interval time by ElementID.
In order for the function to be executed until the final position needs to call settimeout to loop execution, many beginners may be stuck in the repeat variable like me. We first look at settimeout (code,millisec), where code can be thoughtstringOrfunction, in this case the author takes the form of a string to pass the value. If we use strings, we need to combine the variables ElementID final_x final_y interval and other constants together to make the following effects:
SetTimeout (moveelement (' ElementID ', final_x,final_y,interval), interval).
That is to say settimeout inside of repeat = moveelement (' ElementID ', final_x,final_y,interval) so splicing before is this:
Moveelement ('
ElementID
‘,
Final_x
,
Final_y
,
Interval
)
The concatenation of strings requires only double quotation marks on each character, and the end result is:

repeat = "moveelement ('" +elementid+ "'," +final_x+ "," +final_y+ "," +interval+ ")";
Pay special attention not to confuse the "".

In addition, if you pass directly to the settimeout variable function, this is the case:
repeat = function () {moveelement (elementid,final_x,final_y,interval)};
This is easier to read and the effect is exactly the same.

JS settimeout function

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.