flynch01
10-01-2008, 11:45 PM
I'm sure it's been brought up before, but this problem is pretty code specific so. Lets go, so. Calling setTimeout inside an object doesn't work due to the this points being different etc etc. My problem is that, setTimeout doesn't seem to work until the function has been called multiple times. Here's the code first:
function textObject( txtPtr ) {
this.text = txtPtr;
this.fadeVal = 0;
this.doFade = 0;
this.fadeOut = function() {
var gObj = this;
if( this.doFade == 0 ) {
this.fadeVal = 1;
this.doFade = 1;
setTimeout( gObj.fadeOut, 500 );
} else {
if( this.fadeVal <= 0 ) {
this.text.style.opacity = 0;
return 0;
}
this.fadeVal = this.fadeVal - 0.04;
this.text.style.opacity = this.fadeVal;
setTimeout( gObj.lolErt, 50 );
}
}
return true;
}
And heres a snippet of HTML I'm using:
<span onclick="OBJ.fateOut()">Click me</span>
I've been placing alerts()'s after each line to see whether the code is being executed, if I place alert() in the } else { section (which should be entered after 500 miliseconds) and then click. After 500 mili seconds, as expected. It alerts me. This means that the setTimeout is infact working. However, the second timeOut doesn't work. No matter what. I have to manually click the text over and over again in order for it to loop. So this means that it isn't working.
I placed an alert after the second setTimeout, and something weird happened. The first time I click the text, the alert appears. Then doesn't appear again, so that means the first timeout worked as before, but the second didn't and so it isn't looping. So I click again, this time the alert shows, and immediately another alert shows. So it looped once.. then not again. Why is this?
Sorry for long post. I know I could easily solve this by using a loop, and I probably will use a loop after this, but now that I've found this problem, I would like to know what is causing it. :D
Edit:
There shouldn't be anything wrong with the function, I just rewrote as a normal function, couple of differences but nothing that should change whether it works or not (user global variables and parameters instead of class parameters.) but this works.
var fadeCount = 0;
var textStore = 0;
function fadeText( txtPtr, doFade ) {
if( doFade == 0 ) {
textStore = txtPtr;
fadeCount = 1;
setTimeout( "fadeText( 0, 1 )", 500 );
} else {
if( fadeCount <= 0 ) {
textStore.style.opacity = 0;
return 0;
}
fadeCount = fadeCount - 0.04;
textStore.style.opacity = fadeCount;
setTimeout( "fadeText( 0, 1 )", 50 );
}
}
function textObject( txtPtr ) {
this.text = txtPtr;
this.fadeVal = 0;
this.doFade = 0;
this.fadeOut = function() {
var gObj = this;
if( this.doFade == 0 ) {
this.fadeVal = 1;
this.doFade = 1;
setTimeout( gObj.fadeOut, 500 );
} else {
if( this.fadeVal <= 0 ) {
this.text.style.opacity = 0;
return 0;
}
this.fadeVal = this.fadeVal - 0.04;
this.text.style.opacity = this.fadeVal;
setTimeout( gObj.lolErt, 50 );
}
}
return true;
}
And heres a snippet of HTML I'm using:
<span onclick="OBJ.fateOut()">Click me</span>
I've been placing alerts()'s after each line to see whether the code is being executed, if I place alert() in the } else { section (which should be entered after 500 miliseconds) and then click. After 500 mili seconds, as expected. It alerts me. This means that the setTimeout is infact working. However, the second timeOut doesn't work. No matter what. I have to manually click the text over and over again in order for it to loop. So this means that it isn't working.
I placed an alert after the second setTimeout, and something weird happened. The first time I click the text, the alert appears. Then doesn't appear again, so that means the first timeout worked as before, but the second didn't and so it isn't looping. So I click again, this time the alert shows, and immediately another alert shows. So it looped once.. then not again. Why is this?
Sorry for long post. I know I could easily solve this by using a loop, and I probably will use a loop after this, but now that I've found this problem, I would like to know what is causing it. :D
Edit:
There shouldn't be anything wrong with the function, I just rewrote as a normal function, couple of differences but nothing that should change whether it works or not (user global variables and parameters instead of class parameters.) but this works.
var fadeCount = 0;
var textStore = 0;
function fadeText( txtPtr, doFade ) {
if( doFade == 0 ) {
textStore = txtPtr;
fadeCount = 1;
setTimeout( "fadeText( 0, 1 )", 500 );
} else {
if( fadeCount <= 0 ) {
textStore.style.opacity = 0;
return 0;
}
fadeCount = fadeCount - 0.04;
textStore.style.opacity = fadeCount;
setTimeout( "fadeText( 0, 1 )", 50 );
}
}