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

145
Views
can't bind this to function

I have a DOM element btn and have function with setTimeout which listens on this element. I want to remove my listener while function is running to prevent multiple calls of setTimeout and time overlapping, but this in my callback function is window? I tried to use bind but it's not working

const checkTable = () => {
  this.removeEventListener('click', checkTable);
  console.log(this); // i get "window"
  setTimeout(() => {
     //some code
  }, 3000);
};

const app = () => {
  const checkBtn = document.querySelector('.check-btn');
  checkBtn.addEventListener('click', checkTable.bind(checlBtn))
};

app();

and the same result with onclick event

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

0

You can't call .bind() on an arrow function. Arrow functions are used to automatically use the this value from where it's defined. For this to work, your checkTable needs to be a "normal" function.

function checkTable() {
  this.removeEventListener('click', checkTable);
  console.log(this); // i get "window"
  setTimeout(() => {
     //some code
  }, 3000);
}
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!