为什么不能在自引用函数调用中向setTimeout传递参数呢?
Why can't you pass arguments into setTimeout in a self referencing function call?
我认为有更好的方法,也许有人可以指出我的错误。我通过上下计数来脉冲box-shadow blur css属性,让函数在启动后调用自己。
由于某些原因,我不能将第一次传递的参数传递给setTimeout调用。
关于如何更干净地写这个有什么想法吗?它可以工作,但它甚至可以推广到其他css属性的动画。
var pulseBoxShadowBlurCounter = 0;
var pulseBoxShadowBlurDirection;
$(document).ready(function() { // dom binds
pulseBoxShadowBlur($('#getData-btn'),14,'#fff',100); // start the process
});
function pulseBoxShadowBlur(pulseElement,max,color,delayTime){
var cssInput = '0px 0px '+pulseBoxShadowBlurCounter+'px '+color;
$(pulseElement).css('box-shadow', cssInput);
if(pulseBoxShadowBlurCounter == max){
pulseBoxShadowBlurDirection = 1; // backwards
}
if(pulseBoxShadowBlurCounter == -5){ // negative num for pause at 0 time
pulseBoxShadowBlurDirection = 0; // forward
}
if(pulseBoxShadowBlurDirection == 0){
pulseBoxShadowBlurCounter++;
}else{
pulseBoxShadowBlurCounter--;
}
setTimeout( "pulseBoxShadowBlur($('#getData-btn'),14,'#fff',100);",delayTime ); // loop
}
您可以:
setTimeout(function() { pulseBoxShadowBlur(element, max, color, delayTime); }, delayTime);
一般来说,传递一些JavaScript代码的字符串版本作为setTimeout()
或setInterval()
的第一个参数是一个坏主意。
必须在闭包中完成,像这样:
setTimeout( function() {
pulseBoxShadowBlur($('#getData-btn'),14,'#fff',100);
}),delayTime );
因为你正在使用jQuery,你可以使用delay
与queue
这样:
$('#getData-btn').delay(delayTime).queue(function(next) {
pulseBoxShadowBlur($(this),14,'#fff',100);
next(); // resume queue
});
相关文章:
- setTimeout刷新,毫秒参数不起作用
- toString似乎导致setTimeout不总是为toString的变量参数执行函数
- setTimeout向函数参数添加条件
- js:无法将参数传递给setTimeOut中的匿名函数
- 如何将参数传递给 setTimeout() 回调
- 将参数传递到闭包中以进行 setTimeout
- 如何将参数传递给使用 setTimeout 调用的函数
- Javascript:将参数传递给内部setTimeOut函数
- 函数的参数,在 javascript 中依次用作 setTimeout 函数的参数
- 为什么setTimeout和setInterval's回调第一个参数
- 对setTimeout中的第一个参数感到困惑
- 如何在遍历对象时为setTimeout参数设置固定的时间偏移量
- 如何将参数传递到setTimeout中的函数
- 如何调用带有参数的函数作为setTimeout回调
- setTimeout和带参数的递归函数
- 可以't传递匿名函数的参数,该参数作为setTimeout函数传递
- Javascript setTimeout参数问题(I'm使用匿名函数版本)
- setTimeout传递参数问题
- setTimeOut参数正在传递
- 我怎么会有'this'setTimeout参数中的关键字