Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

233
Views
callback function (arrow) and class method in TimeOut

I want to trigger 2 functions with a unique timeOut.
The following works fine

    setTimeout(function() {
      function1(); // runs first
      function2(); // runs second
    }, 1000)

But in my case, I have a class method and an callback (arrow function).
It looks like this (minimalized):

    class Timer {
      constructor(callback) {
        this.active = false;
        this.callback = callback;
      }

      cancel() {
        this.active = false;
      }

      set() {
        this.active = true;
        this.timeOut = setTimeout( function() {
          this.cancel();
          this.callback; // HERE is my problem. doesn't run, not casted as a function
        }, 1000)
      }

I get the following error Expected an assignment or function call and instead saw an expression. Any trick to fix that?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You are not calling callback function, you can also add type check before calling.

set() {
  this.active = true;
  this.timeOut = setTimeout(() => {
    this.cancel();
    typeof this.callback === 'function' && this.callback();
  }, 1000)
}
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!