Javascript 设置超时功能不工作时Div不透明度更改

Javascript 设置超时功能不工作时Div不透明度更改,javascript,html,css,Javascript,Html,Css,我试图在给定的时间间隔内更改div的不透明度,以便对象始终保持脉冲。该元件将持续脉冲,直到按下按钮。下面的代码没有让元素不断改变不透明度,我不知道为什么 function setRandomZoneOpacity(){ while(buttonpressed==false;){ var n=randomIntFromInterval(0,1); var zone_string = zones[n]; document.getElementB

我试图在给定的时间间隔内更改div的不透明度,以便对象始终保持脉冲。该元件将持续脉冲,直到按下按钮。下面的代码没有让元素不断改变不透明度,我不知道为什么

function setRandomZoneOpacity(){
    while(buttonpressed==false;){
        var n=randomIntFromInterval(0,1);
        var zone_string = zones[n];
        document.getElementById(zone_string).style.filter="opacity(100%)";
        setTimeout(function(){};,1000);
        document.getElementById(zone_string).style.filter="opacity(0%)";
        setTimeout(function(){};,1000);
    }
};
function randomIntFromInterval(min,max){ //random number generator
    return Math.floor(Math.random()*(max-min+1)+min);
}

setTimeout不正确

setTimeout(function(){ //your code here }, 1000);
setTimeout(回调,延迟)
不停止脚本执行
delay
毫秒。它所做的是在
延迟
毫秒后,将要执行的
回调
函数排队

您可以这样做:

setTimeout(function(){
    document.getElementById(zone_string).style.filter="opacity(100%)";
}, 1000);
setTimeout(function(){
    document.getElementById(zone_string).style.filter="opacity(0%)";
}, 2000);
。。。或者这个:

setTimeout(function(){
    document.getElementById(zone_string).style.filter="opacity(100%)";
    setTimeout(function(){
        document.getElementById(zone_string).style.filter="opacity(0%)";
    }, 1000);
}, 1000);

你想过用CSS动画和JS来添加/删除类吗?@ecg8,这样我就可以为每个脉冲修改每个div的CSS了?(脉冲是随机的,但一个接一个)为什么到处都有分号?在
while
条件下,在
setTimeout
功能之后?