1

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?

teachengl
  • 11
  • 1
  • The error message is "illegal invocation". See https://stackoverflow.com/questions/10743596/why-are-certain-function-calls-termed-illegal-invocations-in-javascript. The reason is that "reload" method needs context (which location object it's attached to). You could use `window.location.reload.bind(window.location)` but that's longer than your alternative. – James Apr 06 '22 at 15:43

3 Answers3

2

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.

Ruan Mendes
  • 90,375
  • 31
  • 153
  • 217
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);
Istvan Tabanyi
  • 743
  • 5
  • 12
  • Did you mean to use `Function.bind`? Your code is trying to call the function immediately – Ruan Mendes Apr 06 '22 at 15:42
  • You can examine the difference as it will be called with different scope. Bind can solve the issue if you want to pass reload as a reference – Istvan Tabanyi Apr 06 '22 at 15:52
  • Sorry, I don't understand how one would use your suggestion, you would still have to wrap it in a function and you are only adding keystrokes. – Ruan Mendes Apr 06 '22 at 15:55
  • Suggestion? It was an answer to the original question why it is not working. It is a scoping issue, the answer was already in the question. He didnt ask how to make it work, but why it is not working that way. – Istvan Tabanyi Apr 06 '22 at 15:57
  • You said "Try this". That is, you suggested that code. You probably meant to say that what will actually happen is the same as the first line but what they want is the second line. It is confusing. – Ruan Mendes Apr 06 '22 at 16:00
  • Yes try this to see what is the problem, that was what I meant:) looks like not the best wording. – Istvan Tabanyi Apr 06 '22 at 16:02
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).

Ahendall
  • 13
  • 1
  • 6