Stop SetInterval once response from server is received

I need to check if a data response is sent from my server and then stop the SetInterval function. The server response will happen at a random time.

My SetInterval(() function continues to cycle through even once a data response from the server is received.

How can I stop SetInterval from firing once a data response is received? Or is there a better way to check if my server has sent a response? :sweat:


    ionViewDidEnter() {

          setInterval(() => {    
              let headers = new Headers();
              headers.append('Content-Type', 'application/json');
              let checkUserJson = 'http://localhost:9239/auth-received-check';

              var usernameData = JSON.stringify({username: this.username});

              this.http.post(checkUserJson, usernameData, {headers: headers})
                .map(res => res.json())
                .subscribe(data => {
                  console.log(data);
                }, error => {
                 console.log("could not get json data");
                });

          }, 5000); 
    }

Use clearInterval() function after get response from server. Check this link
https://www.w3schools.com/jsref/met_win_clearinterval.asp

1 Like

Absolutely. Look into the timeout operator.

So what are you really trying to achieve with setInterval?
Imho if you are just trying to make sure your code retries several times and on successful result stops - there are better patterns to achieve that, Can you share what is that you are trying to achieve?

Exactly what you just said - I have the setInverval in place so that it keeps retrying until it gets a successful result.

I have an external JSON file that will get created on a website, and then my nodejs server does a check to see if it can access the file - the post request in my iconic file (shown in initial post) checks the server to see if accessing the file was successful, and if so, the server returns a response.

So all the code is working, it’s just probably a dodgy solution on the ionic side…I must add that this won’t ever be production code and its purely for me playing around with, but I just wanted to get some advice on other methods I could use to achieve the above.

It sounds like you are trying to reinvent push notifications.

I ended up going with the clearInterval option suggested by @anandkumar100 as it was the quickest to implement.

I’m having errors with clearing the interval now though…

“Property ‘interval’ does not exist on type…”

Any suggestions? :slight_smile:


    ionViewDidEnter() {


         this.interval = setInterval(() => {
          
          console.log("Do stuff...")  

          }, 5000); 
    }


}

and then clearing the interval…

 public successMsg() {
      clearInterval(this.interval);
    
}
1 Like

Ok figured it out - I combined the clearInterval into the setInterval function. Maybe this might help someone in future who googles a simple function for setInterval and clearInterval in Ionic 3:

        let id = setInterval( () => {

          console.log("do stuff...")  

          if (some.condition = true) {
            clearInterval(id);
          }
        }, 5000);

1 Like

define interval above like
interval: number;