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
JavaScript

Avatar of undefined
Last Comment
rgarimella

8/22/2022 - Mon
Tom Beck

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

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
Goodangel Matope

Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes
Tom Beck

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?
rgarimella

ASKER
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
COBOLdinosaur

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&
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
ASKER CERTIFIED SOLUTION
Tom Beck

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
rgarimella

ASKER
My bad. You are right it worked.

Thanks