Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

176
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda