argument concatenation in a recursive function

Wahid Polin

The example is from the book "dom scripting" - jeremy keith.

function moveElement(elementID,final_x,final_y,interval) { 
if (!document.getElementById) return false; 
if (!document.getElementById(elementID)) return false; 
var elem = document.getElementById(elementID); 
var xpos = parseInt(elem.style.left); 
var ypos = parseInt(elem.style.top); 
if (xpos == final_x && ypos == final_y) { 
return true; 
} 
if (xpos < final_x) { 
xpos++; 
} 
if (xpos > final_x) { 
xpos--; 
} 
if (ypos < final_y) { 
ypos++; 
} 
if (ypos > final_y) { 
ypos--; 
} 
elem.style.left = xpos + "px"; 
elem.style.top = ypos + "px"; 
var repeat = "moveElement('"+elementID+"',"+final_x+","+final_y+","+interval+")"; 
movement = setTimeout(repeat,interval);
}

I don't understand why there's so many concatenation in this line

var repeat = "moveElement('"+elementID+"',"+final_x+","+final_y+","+interval+")"; 
Matt

It's because var repeat is actually a string representation of a function call, rather than a function itself.

To do the same thing in code (not using a string) you'd do something like this:

    var repeat = function() { moveElement(elementId, final_x, final_y, interval); }
    movement = setTimeout(repeat, interval);

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

분류에서Dev

recursive function building in R

분류에서Dev

Recursive PHP Function

분류에서Dev

Memoise on recursive function

분류에서Dev

Recursive function with IO

분류에서Dev

Pass a function with a set argument

분류에서Dev

Passing a function argument to ddply

분류에서Dev

Tail Recursive Factorial Function in Scala

분류에서Dev

Returning a value from a recursive function

분류에서Dev

C recursive function to calculate Factorial

분류에서Dev

Robotics - Recursive function for fractal.

분류에서Dev

Measuring code complexity of a recursive function

분류에서Dev

C# Recursive function approach?

분류에서Dev

Recursive call to a function inside a class

분류에서Dev

Argument list when calling a function

분류에서Dev

when to pass a pointer argument to a function

분류에서Dev

TypeError: argument of type 'function' is not iterable

분류에서Dev

Pass formset as an argument to a function in Django

분류에서Dev

Argument controller is not a function, got undefined

분류에서Dev

Pass function argument to defined variable

분류에서Dev

C Ways To Pass Function As Argument To Function

분류에서Dev

Writing a recursive function for renaming terms in lambda calculus

분류에서Dev

Using malloc and free in recursive file scanning function

분류에서Dev

Python recursive function returns None instead of value

분류에서Dev

Recursive function not looping through all the children

분류에서Dev

Jump out of deeply recursive function call

분류에서Dev

PHP recursive function not setting nested arrays

분류에서Dev

Trouble with type matching and IO in a recursive function

분류에서Dev

2 dimensional maze solver recursive function

분류에서Dev

UL LI to HTML menu structure with recursive function