Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/87.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Jquery 网页背景彩色闪光灯_Jquery_Html_Css_Background Color - Fatal编程技术网

Jquery 网页背景彩色闪光灯

Jquery 网页背景彩色闪光灯,jquery,html,css,background-color,Jquery,Html,Css,Background Color,有人能告诉我为什么这个代码没有在两种颜色之间闪烁我网页的背景颜色吗 <script type="text/javascript"> function blinkit() { intrvl = 0; for (nTimes = 0; nTimes < 3; nTimes++) { intrvl += 1000; setTimeout("document.bgColor='#0000FF';", intrvl); in

有人能告诉我为什么这个代码没有在两种颜色之间闪烁我网页的背景颜色吗

<script type="text/javascript">
function blinkit() {
    intrvl = 0;
    for (nTimes = 0; nTimes < 3; nTimes++) {
        intrvl += 1000;
        setTimeout("document.bgColor='#0000FF';", intrvl);
        intrvl += 1000;
        setTimeout("document.bgColor='#FFFFFF';", intrvl);
    }
}
</script>

函数blinkit(){
intrvl=0;
对于(nTimes=0;nTimes<3;nTimes++){
intrvl+=1000;
setTimeout(“document.bgColor='#0000FF';”,intrvl);
intrvl+=1000;
setTimeout(“document.bgColor='#FFFFFF';”,intrvl);
}
}
试试这个:

function blinkit() {
    intrvl = 0;
    window.setInterval(function(){
        intrvl += 1000;
        setTimeout("document.bgColor='#0000FF';", intrvl);
        intrvl += 1000;
        setTimeout("document.bgColor='#FFFFFF';", intrvl);
    }, intrvl);
}

永远不要将字符串传递给
setTimeout
,因为它与
eval
一样糟糕

相反,请尝试以下方法:

function blinkit(times, thenwhat) {
    var toggle = times*2, timer = setInterval(function() {
            document.body.style.backgroundColor = toggle%2 ? "#0000FF" : "#FFFFFF";
            toggle--;
            if( !toggle) {
                clearInterval(timer);
                thenwhat && thenwhat();
            }
        },1000);
    return timer;
}
var flashy = blinkit(3);
// The background will flash three times.
// You can also cancel it with `clearInterval(flashy);`
使用上述代码,您还可以告诉它在完成时执行某些操作:

var flashy = blinkit(3,function() {alert("Hello!");});

它在Firefox中为我工作。可能是因为您没有调用
blinkit()
函数?在哪里调用
blinkit
?所显示的只是正在定义的函数,而不是调用的函数body@user182,您可以发布您的代码,包括对blinkit的调用吗?使用while循环。只要说,
while(1)
,因为它将永远有效。但是,您考虑过使用CSS3动画吗?