1

I need to delay call of function for few seconds. I have solution which work but enough good.

  public recieveNewContact() {
      this.allData();
      setTimeout(() => {  
         this.lastAddedItem()
      }, 100)
  }


  public ngOnInit() { 
    this.allData()
  }

  allData() {
      this.accountsService.getContactPerson().subscribe(
         (data) => {
             console.log(data)
        }
      )
    }

This is work good but i don't like this... I need better solution maybe with rxjs ? Delay ?

which is very important.

In this case, I don't need this:

  allData() {
      this.accountsService.getContactPerson().subscribe(
         (data) => {
             console.log(data)
             this.lastAddedItem()
        }
      )
    }

Because i wan't to load lastAddedItem() on init.... Only when is recieveNewContact triggered.

I will try to explain once again:

  public recieveNewContact() {
      this.allData(); 
      this.lastAddedItem()// here to be called when is allData finished without setTimeout
  }
4
  • Why dont you like it? explain the reasons behind needing a different option Commented Aug 20, 2021 at 10:12
  • Here is a simple example. I delayed calling the function for 3 seconds but my http call takes ( need ) 5 seconds to load for example. And then the whole system breaks down because of that. Commented Aug 20, 2021 at 10:15
  • why don't you just set a flag on receiveNewContact? something like callLastAdded which you set to false on your ngOnInit function and to true on your recieveNewContact function. So you would just need to check the flag in your subscribe callback Commented Aug 20, 2021 at 13:01
  • Can you explain the flow here? on ngOnInit you are calling allData() and getting all contact persons. So when exactly are you calling recieveNewContact() and what is lastAddedItem() function? Is it an API call? Commented Aug 20, 2021 at 14:14

1 Answer 1

1

I would suggest to use either delay (if you are 100% sure of the completation time) or finalize (if you want to be sure it will wait no matter how long it takes) (rxjs).

Is it necessary to call allData function everytime since you can keep local copy of data in variable and then do the adjustment on it? Or is lastAddedItem API call?

Also I would suggest to set allData as private since don't want it to be called from teplate or another component.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.