Explain, please, what the heightOK and widthOK properties mean in this script, as well as how the javascript knows that duration is the speed at which the image changes in milliseconds (if it is not decrypted anywhere in the code):

function func(name, width, height, duration, type) { var img = document.images.im, heightOk = 0, widthOk = 0; if (type == true) { if ((h < height) && (w < width)) { if (img.height < height) img.height++; else heightOk = 1; if (img.width < width) img.width++; else widthOk = 1; } if ((h > height) && (w > width)) { if (img.height > height) img.height--; else heightOk = 1; if (img.width > width) img.width--; else widthOk = 1; } if ((h > height) && (w < width)) { if (img.height > height) img.height--; else heightOk = 1; if (img.width < width) img.width++; else widthOk = 1; } if ((h < height) && (w > width)) { if (img.height < height) img.height++; else heightOk = 1; if (img.width > width) img.width--; else widthOk = 1; } if (heightOk + widthOk == 2) setTimeout(func, duration, name, width, height, duration, false); else setTimeout(func, duration, name, width, height, duration, true); } else if (type == false) { if ((h < height) && (w < width)) { if (img.height > h) img.height--; else heightOk = 1; if (img.width > w) img.width--; else widthOk = 1; } if ((h > height) && (w > width)) { if (img.height < h) img.height++; else heightOk = 1; if (img.width < w) img.width++; else widthOk = 1; } if ((h > height) && (w < width)) { if (img.height < h) img.height++; else heightOk = 1; if (img.width > w) img.width--; else widthOk = 1; } if ((h < height) && (w > width)) { if (img.height > h) img.height--; else heightOk = 1; if (img.width < w) img.width++; else widthOk = 1; } if (heightOk + widthOk != 2) setTimeout(func, duration, name, width, height, duration, false); else setTimeout(func, duration, name, width, height, duration, true); } } var h = document.images.im.height; var w = document.images.im.width; var width = Number(prompt("Π’Π΅Π΄ΠΈΡ‚Π΅ ΠΌΠ°ΠΊΡΠΈΠΌΠ°Π»ΡŒΠ½ΡƒΡŽ ΡˆΠΈΡ€ΠΈΠ½Ρƒ")); var height = Number(prompt("Π’Π΅Π΄ΠΈΡ‚Π΅ ΠΌΠ°ΠΊΡΠΈΠΌΠ°Π»ΡŒΠ½ΡƒΡŽ высоту")); var duration = Number(prompt("Π’Π΅Π΄ΠΈΡ‚Π΅ milliseconds")); func('i', width, height, duration, true); 
  • The code is not readable, use code insertion or PasteBin - Perkovec

1 answer 1

widthOk and heightOk in this case work as stop flags. With a value equal to unity width-height does not change.

The author implemented the delay between change cycles through setTimeout(func, duration, name, width, height, duration, true);

The code though works, IMHO, is terrible.

The essence of this text can be read like this:

 ... if ((h == height) && (w == width)) return; // Π²Ρ‹Ρ…ΠΎΠ΄ ΠΈΠ· Ρ†ΠΈΠΊΠ»Π° (img.height < height) ? img.heigth++ : img.heigth--; (img.width < width) ? img.width++ : img.width--; ... setTimeout...