0

I would like to use saveAsDraft function after set timeout process. But I get an error. What do I use for this? Callback, promise or ...

  export class EditBlogComponent implements OnInit {
    ...
    //For draft save
    typingTimer:any;
    doneTypingInterval = 5000;
    ....

    saveAsDraft(blog:Blog) {
       console.log("Taslak kaydediliyor.", this.blog);
    }

    draftSecond(formValue: string) {
        clearTimeout(this.typingTimer);
        this.typingTimer = setTimeout(saveAsDraft(this.blog), 
        this.doneTypingInterval);
    }
  }

Image;

enter image description here

1 Answer 1

1

Your call to saveAsDraft should be inside a function.

The syntax is -

setTimeout(function(){ ... }, timeInMilliseconds);

So your call should be -

setTimeout(function(){
     saveAsDraft(this.blog);
}, timeInMilliseconds);

Additionally, just check if this is available inside the function(). It might not be. so you can do like -

var refToThis = this;
setTimeout(function(){
     refToThis.saveAsDraft(refToThis.blog);
}, timeInMilliseconds);

or use arrow functions

setTimeout(()=>{
     this.saveAsDraft(this.blog);
}, timeInMilliseconds);

You can not access saveAsDraft without this since that is a class member and can only be accessed using its object.

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

4 Comments

When I do like this, I got an error. "Cannot find name 'saveAsDraft'"
That's because the saveAsDraft() is a class function.
Thank you. The problem solved
Glad I could help :)

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.