[英]setTimeout function in javascript is being called but not setting timeout
var r = 0;
var g = 0;
var b = 225;
function gradient() {
b = b - 10;
}
while (b> 6){
setTimeout(gradient(),1000);
}
ctx.beginPath();
ctx.fillStyle= 'rgb('+r+', '+g+', '+b+')'
ctx.rect(0,0,1200,590,2*Math.PI)
ctx.stroke()
ctx.fill();
ctx.closePath();
我知道while
循环和函数正在工作,因为变量b
减少并且画布图像的颜色发生了变化。 但是没有延迟,它会立即发生。 我也尝试过setInterval
函数,但我无法让它工作。
尝试这个:
var r = 0;
var g = 0;
var b = 225;
function gradient() {
if(b > 6){
console.log(b);
b = b - 10;
setTimeout(gradient,1000);
}
}
gradient();
ctx.beginPath();
ctx.fillStyle= 'rgb('+r+', '+g+', '+b+')'
ctx.rect(0,0,1200,590,2*Math.PI)
ctx.stroke()
ctx.fill();
ctx.closePath();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.