【发布时间】:2014-08-08 15:31:35
【问题描述】:
我想每秒更改一次正方形的颜色 (#myID: width = height = 100px)。 (为了检查开关循环是否有效,在每个“案例”中我都写了 console.log("smth occurred");。) 但是这个正方形的颜色并没有改变。 "FIDDLE"
接下来,每隔一秒 document.getElementById('myID') 都会写入一个新形成的变量 thesquare。如何在函数之外使变量成为全局变量?
Javascript:
var i = 0;
function changecolor()
{
var thesquare = document.getElementById('myID');
switch (i)
{
case 0 :
thesquare.setAttribute("background-color","red");
++i;
break;
case 1 :
thesquare.setAttribute("background-color","green");
++i;
break;
default :
thesquare.setAttribute("background-color","blue");
i=0;
break;
}
}
setInterval("changecolor()",1000);
【问题讨论】:
-
setInterval(changecolor,1000); -
这个“纯 javascript怎么样?它使用DOM api。
-
纯 Javascript - 无 jQuery
-
@Bergi 我想你明白他想要表达的意思。我想这意味着在这种情况下“没有框架”。
标签: javascript animation global-variables setinterval