简体   繁体   English

JavaScript setInterval

[英]JavaScript setInterval

This is an example from a tutorial about setInterval, but it doesn`t explain it sufficiently for my newbie brain. 这是关于setInterval的教程中的一个例子,但它并没有为我的新手大脑解释它。 I would appreciate if you could answer these questions 如果你能回答这些问题,我将不胜感激

i) does the 1000 millesecond timer mean that moveElement function will be triggered every second? i)1000 millesecond计时器是否表示每秒触发moveElement函数? In other words, after it has run, it will wait 1 second and then run it again? 换句话说,在它运行后,它将等待1秒然后再次运行它?

ii) is the purpose of moveElement to move the "redBox" 10 pixels to the left each time it runs? ii)moveElement的目的是每次运行时将“redBox”向左移动10个像素? is that why "px" is used in the function 这就是为什么在函数中使用“px”的原因

iii) after moveElement runs for the first time, does the new value for x (x+=10) replace the value of 0 in var x=0? iii)moveElement第一次运行后,x(x + = 10)的新值是否替换var x = 0中的0值? ie does it get stored outside of the function in the variable x at the top of the program? 即它是否存储在程序顶部的变量x中的函数之外?

var x = 0;
setInterval(moveElement,1000);

function moveElement() {
  x+=10;
  var left = x + "px";
  document.getElementById("redbox").style.left=left;

i) Yes, at least in theory. i)是的,至少在理论上。 JavaScript's ( mostly ) single threaded nature mean it won't be exactly 1000ms. JavaScript( 大部分 )单线程性质意味着它不会完全是1000毫秒。

ii) It moves it 10px to the right, by adding 10px to the left. ii)通过向左添加10px将其向右移动10px。 Px is short for pixels, which is short for picture elements. Px是像素的缩写,是像素的缩写。

iii) x is defined outside of the function, so it persists each time. iii) x在函数之外定义,因此每次都会持续存在。 Every time the function is called, x will be 10 larger. 每次调用该函数时, x都会大10。 Had x been defined within the function, it will be 10 each time it is called. 如果在函数中定义了x ,则每次调用它都会为10。

i) the setInterval will run the moveElement function every second. i)setInterval将每秒运行moveElement函数。 if it were setTimeout it would only run it once after 1 second. 如果是setTimeout,它只会在1秒后运行一次。

ii) seems like thats what it does. ii)看起来就是它的作用。

iii) in this case x is not declared anywhere in the function moveElement so then it tries to find a global variable which it does at the top. iii)在这种情况下,x未在函数moveElement中的任何位置声明,因此它会尝试查找它在顶部执行的全局变量。 So yes it will store the new value in the x outside of the function. 所以是的,它会将新值存储在函数外部的x中。

i) This will help you understand setTimeout and setInterval : http://www.elated.com/articles/javascript-timers-with-settimeout-and-setinterval/ i)这将帮助您理解setTimeout和setInterval: http//www.elated.com/articles/javascript-timers-with-settimeout-and-setinterval/

ii) "px" is added ( it means "pixels") to assign valid attribute value to "style.left" which has the unit "px" . ii)添加“px”(表示“像素”)以将有效属性值分配给具有单位“px”的“style.left”。

iii) Yes it replaces the value, as var x has been declared outside the function,and thus is a global variable. iii)是的,它取代了值,因为var x已在函数外声明,因此是一个全局变量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM