Link to home
Start Free TrialLog in
Avatar of rgarimella
rgarimella

asked on

Javascript Timer

Hi Folks,

I have the following code:

var isReady = Initialize();
	if(isReady){
		initSession(); 
	}

Open in new window


Because of network latency, initially I get a False value and after 2-5 seconds i get a True value.

How can I check the value of "isReady" on  a timer, so that the isReady section of the code is executed when true?

Thanks

RG
Avatar of Tom Beck
Tom Beck
Flag of United States of America image

This would check Initialize every second until it returned true.

var t = setInterval(Initialize, 1000);
function Initialize(){
	if(<condition true>){
		clearInterval(t);
	    initSession();
	}
}

Open in new window

Avatar of rgarimella
rgarimella

ASKER

How would you modify the code so that you dont check Initialize, but the value of t.

The reason being, I can only call the Initialize function once, otherwise it returns a error on my system.

I need to be able to check the Value of t, every second and once it turn true the initSession function is called.

Thanks

RG
How would you modify the code so that you dont check Initialize, but the value of t.

That's not making sense to me. The value of t is just the setInterval itself. It seems to me that you can only call initSession() once. You should be able to call Initialize() as many times as needed until some condition inside is true, then stop calling Initialize() and call initSession().

Can you show us your version of Initialize()? How does it return true?
Here is the problem for me.

Initialize function initial value is false. After 2-3 sec the value of the Initialize function turns to true.

So Instead of calling the Initialize function many times via setInterval, I was wondering if we can check for the value stored in a variable from the initial Initialize.

Thanks
This does not seem logical to me.  Are you doing it as part of initial page load? If yes then why would the onload event not work.

if not then the code already posted by tommyboy should work.  getting an error because you call the function more than once, sounds like something from another planet, and I really think we need to see your whole code which almost certainly has to contain logic errors.

Cd&
ASKER CERTIFIED SOLUTION
Avatar of Tom Beck
Tom Beck
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
My bad. You are right it worked.

Thanks