JavaScript如果计数
我想我一定错过了一些非常明显的东西。使用这段代码,我想计算并触发定时器的背景变化;但是它不传递变量,每次计时器调用函数时,它都传递一个未定义的变量 值得注意:JavaScript如果计数,javascript,function,timer,Javascript,Function,Timer,我想我一定错过了一些非常明显的东西。使用这段代码,我想计算并触发定时器的背景变化;但是它不传递变量,每次计时器调用函数时,它都传递一个未定义的变量 值得注意: 查看控制台-没有错误 我知道Jquery可能会更好,但由于一些令人讨厌的遗留问题(这是一个完全不同的问题),该站点无法使用Jquery 代码 <script> /* Get the body element */ var body = document.getElementsByTagName('body')[0]; /*
- 查看控制台-没有错误
- 我知道Jquery可能会更好,但由于一些令人讨厌的遗留问题(这是一个完全不同的问题),该站点无法使用Jquery
<script>
/* Get the body element */
var body = document.getElementsByTagName('body')[0];
/* Call the function */
changebackground();
/* Set the starting count */
var changecount = 1;
/* Change function */
function changebackground(changecount) {
if(changecount=1){
body.style.backgroundImage = 'url(/templates/images/background_1.jpg)';
changecount = 2;
}
else if (changecount=2) {
body.style.backgroundImage = 'url(/templates/images/background_2.jpg)';
changecount = 3;
}
else {
body.style.backgroundImage = 'url(/templates/images/background_3.jpg)';
changecount = 1;
}
setInterval(function(changecount){changebackground();},3000);
}
</script>
/*获取body元素*/
var body=document.getElementsByTagName('body')[0];
/*调用函数*/
changebackground();
/*设置开始计数*/
var-changecont=1;
/*变换函数*/
函数changebackground(changecount){
如果(更改计数=1){
body.style.backgroundImage='url(/templates/images/background_1.jpg)';
更改计数=2;
}
否则如果(更改计数=2){
body.style.backgroundImage='url(/templates/images/background_2.jpg)';
更改计数=3;
}
否则{
body.style.backgroundImage='url(/templates/images/background_3.jpg)';
changecount=1;
}
setInterval(函数(changecount){changebackground();},3000);
}
您必须将参数放在函数的括号之间:changebackground(changecont)
(也是第一次调用函数时)必须将参数放在函数的括号之间:
changebackground(changecont)
(也是在您第一次调用函数时)将
changecont
作为参数删除到changebackground()。它是一个全局变量,因此无需在参数列表中列出
function changebackground() {
...
}
然后将setInterval调用更改为:
setInterval(changebackground, 3000);
并将调用移到changebackground()之外。您只需要调用setInverval()一次,而不是每次调用changebackground()
最后,使用=
进行比较,而不是=
if (changecount == 1) {
body.style.backgroundImage = 'url(/templates/images/background_1.jpg)';
changecount = 2;
}
else if (changecount == 2) {
body.style.backgroundImage = 'url(/templates/images/background_2.jpg)';
changecount = 3;
}
else {
body.style.backgroundImage = 'url(/templates/images/background_3.jpg)';
changecount = 1;
}
将
changecont
作为参数删除到changebackground()。它是一个全局变量,因此无需在参数列表中列出
function changebackground() {
...
}
然后将setInterval调用更改为:
setInterval(changebackground, 3000);
并将调用移到changebackground()之外。您只需要调用setInverval()一次,而不是每次调用changebackground()
最后,使用=
进行比较,而不是=
if (changecount == 1) {
body.style.backgroundImage = 'url(/templates/images/background_1.jpg)';
changecount = 2;
}
else if (changecount == 2) {
body.style.backgroundImage = 'url(/templates/images/background_2.jpg)';
changecount = 3;
}
else {
body.style.backgroundImage = 'url(/templates/images/background_3.jpg)';
changecount = 1;
}
您已将
changecont
设置为全局变量。setInterval不会将changecount传递到函数中
试试这个:
setInterval(function(){
changebackground(changecount);
},3000);
您还错误地重新分配了if块中的变量。请改用比较运算符:
if(changecount == 1){
}
您已将
changecont
设置为全局变量。setInterval不会将changecount传递到函数中
试试这个:
setInterval(function(){
changebackground(changecount);
},3000);
您还错误地重新分配了if块中的变量。请改用比较运算符:
if(changecount == 1){
}
您应该使用
setTimeout
而不是setInterval
从函数内部重复调用函数:
function changebackground(changecount) {
.
.
.
setTimeout("changebackground("+changecount+")",3000); // change to this
}
也可以使用=
进行检查,而不是像以下那样使用=
:
if(changecount==1){ ... }
您应该使用
setTimeout
而不是setInterval
从函数内部重复调用函数:
function changebackground(changecount) {
.
.
.
setTimeout("changebackground("+changecount+")",3000); // change to this
}
也可以使用=
进行检查,而不是像以下那样使用=
:
if(changecount==1){ ... }
您没有将changecount传递给匿名函数,而是将其定义为一个参数。请改用以下代码:
<script>
/* Get the body element */
var body = document.getElementsByTagName('body')[0];
/* Call the function */
changebackground();
/* Set the starting count */
var changecount = 1;
/* Change function */
function changebackground() {
if(changecount=1){
body.style.backgroundImage = 'url(/templates/images/background_1.jpg)';
changecount = 2;
}
else if (changecount=2) {
body.style.backgroundImage = 'url(/templates/images/background_2.jpg)';
changecount = 3;
}
else {
body.style.backgroundImage = 'url(/templates/images/background_3.jpg)';
changecount = 1;
}
setInterval(changebackground,3000);
}
</script>
/*获取body元素*/
var body=document.getElementsByTagName('body')[0];
/*调用函数*/
changebackground();
/*设置开始计数*/
var-changecont=1;
/*变换函数*/
函数changebackground(){
如果(更改计数=1){
body.style.backgroundImage='url(/templates/images/background_1.jpg)';
更改计数=2;
}
否则如果(更改计数=2){
body.style.backgroundImage='url(/templates/images/background_2.jpg)';
更改计数=3;
}
否则{
body.style.backgroundImage='url(/templates/images/background_3.jpg)';
changecount=1;
}
设置间隔(changebackground,3000);
}
这将每隔3秒执行一次函数。您不必传递变量,因为它是全局变量。您没有将changecount传递给匿名函数,而是将其定义为一个参数。请改用以下代码:
<script>
/* Get the body element */
var body = document.getElementsByTagName('body')[0];
/* Call the function */
changebackground();
/* Set the starting count */
var changecount = 1;
/* Change function */
function changebackground() {
if(changecount=1){
body.style.backgroundImage = 'url(/templates/images/background_1.jpg)';
changecount = 2;
}
else if (changecount=2) {
body.style.backgroundImage = 'url(/templates/images/background_2.jpg)';
changecount = 3;
}
else {
body.style.backgroundImage = 'url(/templates/images/background_3.jpg)';
changecount = 1;
}
setInterval(changebackground,3000);
}
</script>
/*获取body元素*/
var body=document.getElementsByTagName('body')[0];
/*调用函数*/
changebackground();
/*设置开始计数*/
var-changecont=1;
/*变换函数*/
函数changebackground(){
如果(更改计数=1){
body.style.backgroundImage='url(/templates/images/background_1.jpg)';
更改计数=2;
}
否则如果(更改计数=2){
body.style.backgroundImage='url(/templates/images/background_2.jpg)';
更改计数=3;
}
否则{
body.style.backgroundImage='url(/templates/images/background_3.jpg)';
changecount=1;
}
设置间隔(changebackground,3000);
}
这将每隔3秒执行一次函数。您不必传递变量,因为它是全局变量。很少有错误
比较运算符不正确。您也不需要将变量传递给changebackground()
函数,因为该变量是全局变量
这应该行得通
/* Get the body element */
var body = document.getElementsByTagName('body')[0];
/* Call the function */
changebackground();
/* Set the starting count */
var changecount = 1;
/* Change function */
function changebackground() {
if (changecount == 1) {
body.style.backgroundImage = 'url(/templates/images/background_1.jpg)';
changecount = 2;
} else if (changecount == 2) {
body.style.backgroundImage = 'url(/templates/images/background_2.jpg)';
changecount = 3;
} else {
body.style.backgroundImage = 'url(/templates/images/background_3.jpg)';
changecount = 1;
}
// line just added for debugging
alert(changecount);
setInterval(changebackground(), 3000);
}
几乎没有什么不对的
比较运算符不正确。您也不需要将变量传递给changebackground()
函数,因为该变量是全局变量
这应该行得通
/* Get the body element */
var body = document.getElementsByTagName('body')[0];
/* Call the function */
changebackground();
/* Set the starting count */
var changecount = 1;
/* Change function */
function changebackground() {
if (changecount == 1) {
body.style.backgroundImage = 'url(/templates/images/background_1.jpg)';
changecount = 2;
} else if (changecount == 2) {
body.style.backgroundImage = 'url(/templates/images/background_2.jpg)';
changecount = 3;
} else {
body.style.backgroundImage = 'url(/templates/images/background_3.jpg)';
changecount = 1;
}
// line just added for debugging
alert(changecount);
setInterval(changebackground(), 3000);
}
您的if语句需要是
==
非=
您的if语句需要是==
非=
为什么函数总是被触发。注意警报是如何持续出现的?@tim.baker,因为最后一行setInterval(changebackground(),3000)代码