다음 스크립트를 작동 시키려고합니다.하지만 continueAnimation 함수에 포함될 때 cPreloaderTimeout 변수가 업데이트되지 않고 'Uncaught RangeError : Maximum call stack size 초과했습니다 '. 자바 재귀 호출이 범위 오류를 만듭니다
var loadingIcon = {
cSpeed : 8,
cWidth : 64,
cHeight : 32,
cTotalFrames : 7,
cFrameWidth : 64,
cImageSrc : 'sprites.png',
cImageTimeout : false,
cIndex : 0,
cXpos : 0,
cPreloaderTimeout : false,
SECONDS_BETWEEN_FRAMES : 0,
startAnimation : function() {
document.getElementById('loaderImage').style.backgroundImage='url('+ this.cImageSrc+')';
document.getElementById('loaderImage').style.width=this.cWidth+'px';
document.getElementById('loaderImage').style.height=this.cHeight+'px';
//FPS = Math.round(100/(maxSpeed+2-speed));
FPS = Math.round(100/this.cSpeed);
SECONDS_BETWEEN_FRAMES = 1/FPS;
this.cPreloaderTimeout = setTimeout(this.continueAnimation(), SECONDS_BETWEEN_FRAMES/1000);
},
continueAnimation : function() {
this.cXpos += this.cFrameWidth;
//increase the index so we know which frame of our animation we are currently on
this.cIndex += 1;
//if our cIndex is higher than our total number of frames, we're at the end and should restart
if (this.cIndex >= this.cTotalFrames) {
this.cXpos =0;
this.cIndex=0;
}
if(document.getElementById('loaderImage'))
document.getElementById('loaderImage').style.backgroundPosition=(-this.cXpos)+'px 0';
this.cPreloaderTimeout = setTimeout(this.continueAnimation(), SECONDS_BETWEEN_FRAMES*1000);
},
stopAnimation : function(){//stops animation
clearTimeout(this.cPreloaderTimeout);
this.cPreloaderTimeout = false;
}
}
jQuery(document).ready(function() {
jQuery(document).on("click", "#test", function(){
var loader = loadingIcon;
loader.startAnimation();
setTimeout(loader.stopAnimation(), 3000);
});
});
은 처음에는 일반 자바 스크립트,하지만 나는 그것이 동시에 재사용 여러 번 할 수 있도록에서 개체를 만들기 위해 노력하고있어. 이제 문제는 startAnimation 또는 continueAnimation이 트리거 될 때 cPreloaderTimeout 변수가 올바르게 설정되지 않았기 때문입니다.
'setTimeout'을 올바르게 사용하지 않았기 때문에. 'setTimeout (this.continueAnimation(), ...)'에서'()'을 제거하고'setTimeout'에 대해 더 읽으십시오. –
**주의 : **'setTimeout' 문제를 고칠 때, 당신은 [**'이 이슈 **]에 빠지게 될 것입니다. (https://stackoverflow.com/questions/591269/settimeout-and-this - 자바 스크립트). –