SetInterval and ClearInterval -Javascript

I had been developing a real time chat system at my work  and the chat system takes some time to load(makes an ajax call to the backend that fetches users details,chat message,chat room id ,chat user role id etc..).and also there are multiple chat rooms based on user roles. The challenge was to wait for all of data to load  and then enable the chat system.

Solution :

The solution  is to wait for x interval of time and then make a call x time to check if the data we require is loaded.

 


var intialloading=false;
waitoperation();
var someinterval =setInterval(function(){

if(intialloading)

{

//clear the interval.

ClearInterval(someinterval);

}

},2000) // call function every 2 secs

function waitoperation()

{

//some call

intialloading=true;

}

 

 

 

 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s