Questions about the sample code in Chapter 10 of the Art of JavaScript DOM programming?

< H1 > this code is extracted from page 192 of Chapter 10 of the Art of Javascript DOM programming Edition 2 < / H1 >

what is the function of the penultimate line variable movement? Only the time function can be run. I don"t see where this variable is used. Page 192 says that the incorrect animation is caused by global variables and does not know how to affect it

< H2 > the code is as follows: moveElement.js < / H2 >
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) {
    xposPP;
  }
  if (xpos > final_x) {
    xpos--;
  }
  if (ypos < final_y) {
    yposPP;
  }
  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); 
  //
}

Thank you for your guidance!

May.05,2022

Save the retarder handle, which is used to clear the retarder. If it is not cleared, there will be a memory leak

Menu