3

Why is it that this works,

setTimeout(function() { window.location.reload() }, 3000);

but this doesn't?

setTimeout(window.location.reload, 3000);

I receive the following error: TypeError: 'reload' called on an object that does not implement interface Location.

rohitpaulk
  • 407
  • 6
  • 11

1 Answers1

6

In theory it can be. When you pass it like that, it is just the function, without its execution context (this). Since the function (internally) makes use of this, it fails. You may notice this with console.log as well.

The solution is to bind the context:

setTimeout(window.location.reload.bind(window.location), 3000);
Scimonster
  • 32,893
  • 9
  • 77
  • 89