Why doesn’t this setTimeout work?

Asked

Viewed 354 times

0

My goal is when I click the button, 3 seconds after a div reread the page...

Why does that code not work?

<button id="btn-cadastra-tarefa" ng-click="adicionaTarefa()" onclick="atualiza()" 
        type="button" data-dismiss="modal" >
    <span class="glyphicon glyphicon-ok-sign"></span>Salvar
</button>

<div id="atualizaPagina"></div>

function atualiza() {
    setTimeout(function(){ $('#atualizaPagina').location.reload(); }, 3000);
}
  • I can’t say for sure, but the method location does not extend to HTML elements.

  • "after a div is given F5", what data you want to assign to that div?

  • You want to update the whole page or just the created div?

2 answers

5

Mostly because it doesn’t make sense.

$('#atualizaPagina') returns an HTML element and no HTML element has the property location, consequently, there is no method reload().

If you want to update the whole page use window.location.reload(). There is no method to update a div, because it wouldn’t make sense. It would update based on what?

What you must really want to do is just modify the data that is shown in this div, for that, you need to make an Ajax call pro backend and then update the div via Javascript based on the answer to your Ajax call.

I cannot show an example with code because this part of the code does not exist in the question.

See a generic example that makes an Ajax request and then updates the tag img:

const reqUrl = 'https://api.giphy.com/v1/gifs/search?api_key=9e4393bfcf774cd08e0757b4688b11f7&q=javascript';

$('#ajax').on('click', function() {
  $.get(reqUrl, function(r) {
    $('#img').attr('src', r.data[0].images.original.url);
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button id="ajax">Faça uma requisição Ajax</button><br>

<img id="img" src="http://netcoders.com.br/wp-content/uploads/2015/10/js3.png" width="250" height="250" />

  • Wouldn’t that update the whole page? If I understood correctly, the idea is to update only the div. I think I would need some code ajax for that reason

  • @rLinhares She speaks in "dar F5". Normally this is update the whole page, but by code, it makes sense what you said.

  • Yes, the intention is to give F5 and update the data

2


If really the need is to refresh the page all you can use:

Works in IE:

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

Not tested on all browsers, may not work on some:

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

To reload a page via javascript you can use one of the following options:

Javascript 1.0

window.location.href = window.location.pathname + "pagina";

Creates page history entry

Javascript 1.1

window.location.replace(window.location.pathname + "pagina");

Does not create page history entry

Javascript 1.2

window.location.reload();

Reload the cache page

window.location.reload(false);

Reload the cache page

window.location.reload(true);

Reload server page (force new GET)

  • 1

    Javascript 1.0, 1.1, 1.2? Sorry but this doesn’t seem to make sense, Javascript was a technology proposed by Netscape (old Mozilla), these versions only make logic in it, since nowadays all browsers support these functions, except the true in Reload. Read this: How to find the Javascript version (via code)?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.