سؤال

I have a problem, again jajaja.

var X=0
var Y=0

With this function, i want to add Y to X every second.

function FA(){
    X= X+Y;
    setTimeout(function(){FA()},1000); 
}

With this funcion, i want to add 1 to Y.

function FB()
{
    Y=Y+1;   
} 

Example:

Y=0
X=0
X=0
X=0
[...]
(execute the function FB) 
Y=1
X=1
X=2
X=3
[...]

My problem is that the second time that i execute FB its not +2 to X, its +4 and i want only +2.

هل كانت مفيدة؟

المحلول

Your code looks ok. I'd change some syntax details to make it simpler:

function FA(){
    X += Y;
    setTimeout("FA()",1000); 
}

function FB(){
    Y++;
}

i think the problem may be somewhere in the code you didn't post. it looks like you add Y value twice, so tripple-check if you call FA again. It would cause what you are experiencing now (you may have TWO timeouts).

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top