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

174
Views
setTimeout(window.location.reload,1000) not working

Why does this not work?

setTimeout(window.location.reload, 1000)

This does

setTimeout(() => window.location.reload(), 1000)

The first parameter is a function. Shouldn't the first example work?

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

0

The problem is that you are passing a loose reference to the reload function; therefore, losing its binding to window.location.

You can use the following to prove the point (It may not work

setTimeout(window.location.reload.bind(window.location), 1000)

But I think adding a function wrapper makes it easier to understand.

Example

const a = {
  value: 2,
  b: function() {
    console.log(this.value);
  }
}

a.b();

const b = a.b;
b();

const boundB = b.bind(a);
boundB();

Summary Using syntax like a.b() makes sure that b is called with a as its this.

Calling a function without a . before will the function as a global, which will pass window as this in sloppy mode and null as this in strict mode.

This can always be overriden by binding function or by using arrow functions (which take its this from the surrounding this) or if you bind your function ahead of time.

about 4 years ago · Juan Pablo Isaza Report

0

It won't work as the scope of calling window.location.reload will be window instead of window.location. Try this:

window.location.reload.call(window);

vs.

window.location.reload.call(window.location);
about 4 years ago · Juan Pablo Isaza Report

0

This post sums it up nicely https://stackoverflow.com/a/10840058/10853009

Because reload() needs window.location as this. In other words - it is a method of window.location. When you say:

var fun = window.location.reload;

fun();

You are calling reload() function without any this reference (or with implicit window reference).

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!