JavaScript 设置间隔
这是关于 setInterval 的教程中的一个示例,但它并没有为我的新手大脑充分解释。如果您能回答这些问题,我将不胜感激
i) 1000 毫秒计时器是否意味着 moveElement 函数每秒都会被触发?也就是说,运行完之后,会等1秒,然后再运行?
ii) moveElement 的目的是每次运行时将“redBox”向左移动 10 个像素吗?这就是为什么在函数 iii) 中使用“px”的原因
,在 moveElement 第一次运行后,x (x+=10) 的新值是否会替换 var x=0 中的 0 值?即它是否存储在函数外部程序顶部的变量 x 中?
var x = 0;
setInterval(moveElement,1000);
function moveElement() {
x+=10;
var left = x + "px";
document.getElementById("redbox").style.left=left;
This is an example from a tutorial about setInterval, but it doesn`t explain it sufficiently for my newbie brain. I would appreciate if you could answer these questions
i) does the 1000 millesecond timer mean that moveElement function will be triggered every second? In other words, after it has run, it will wait 1 second and then run it again?
ii) is the purpose of moveElement to move the "redBox" 10 pixels to the left each time it runs? is that why "px" is used in the function
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? i.e. does it get stored outside of the function in the variable x at the top of the program?
var x = 0;
setInterval(moveElement,1000);
function moveElement() {
x+=10;
var left = x + "px";
document.getElementById("redbox").style.left=left;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
i) 是的,至少在理论上是这样。 JavaScript 的(大部分)单线程性质意味着它不会正好是 1000 毫秒。
ii) 通过向左添加 10px,将其向右移动 10px。 Px是pixels的缩写,是picture elements的缩写。
iii)
x
是在函数外部定义的,因此它每次都会持续存在。每次调用该函数时,x
都会增大 10。如果x
在函数内定义,则每次调用时它都会是10。i) Yes, at least in theory. JavaScript's (mostly) single threaded nature mean it won't be exactly 1000ms.
ii) It moves it 10px to the right, by adding 10px to the left. Px is short for pixels, which is short for picture elements.
iii)
x
is defined outside of the function, so it persists each time. Every time the function is called,x
will be 10 larger. Hadx
been defined within the function, it will be 10 each time it is called.i) setInterval 将每秒运行 moveElement 函数。如果是setTimeout,则只会在1秒后运行一次。
ii) 看起来这就是它的作用。
iii) 在这种情况下,x 没有在函数 moveElement 中的任何位置声明,因此它会尝试查找在顶部执行的全局变量。所以是的,它将把新值存储在函数外部的 x 中。
i) the setInterval will run the moveElement function every second. if it were setTimeout it would only run it once after 1 second.
ii) seems like thats what it does.
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. So yes it will store the new value in the x outside of the function.
i) 这将帮助您理解 setTimeout 和 setInterval :
http://www.elated.com/articles/javascript- timers-with-settimeout-and-setinterval/
ii) 添加“px”(意思是“像素”),为“style.left”分配有效的属性值,单位为“px”。
iii) 是的,它替换了值,因为 var x 已在函数外部声明,因此是
一个全局变量。
i) This will help you understand setTimeout and 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" .
iii) Yes it replaces the value, as var x has been declared outside the function,and thus is
a global variable.