-1

I want to reload div content, the results I found on the web didn't work for me. Help me My Code;

var x = new Date();
document.getElementById("refresh").innerHTML=x;
<div id="refresh"> 
</div>
Namysh
  • 3,717
  • 2
  • 9
  • 17
Shosha
  • 1
  • Seems to work fine. The contents of the div with id `refresh` is replaced by the current date. Did you expect it to do something else? – rid Sep 04 '21 at 10:04
  • 1
    I know right now I'm trying to show the date. But I want to renew this process constantly – Shosha Sep 04 '21 at 10:08
  • 1
    You could do the same thing at an interval. Look into [`setInterval()`](https://developer.mozilla.org/en-US/docs/Web/API/setInterval) for that. – rid Sep 04 '21 at 10:09
  • 1
    Does this maybe help you https://stackoverflow.com/questions/27526857/how-to-get-date-to-auto-refresh – anonimostilton Sep 04 '21 at 10:11

3 Answers3

1

So your code should look like this:

setInterval(refresh, 1000);

function refresh() {
    var x = new Date();
    document.getElementById("refresh").innerHTML = x;
}

refresh();

you need to call the function continuously. It didn't work because in your code it only calls it once.

Daki
  • 36
  • 5
0

As my understood, you want to show system date on your html dom, right?

if so, you have to use some button, or you can use setInterval function. For example:

<div id="refresh">
</div>
<script>
  setInterval(function() {
    var x = new Date();
    document.getElementById("refresh").innerHTML = x;
  }, 300);

</script> 
Nyamkhuu Buyanjargal
  • 621
  • 2
  • 11
  • 29
0

your code is fine and working I have checked Please check your Javascript library reference

Here is the working code

<button type="button"
onclick="document.getElementById('refresh').innerHTML = Date()">
Click me to display Date and Time.</button>

<div id="refresh"> 
</div>

For reload, u can use setInterval

<html>
<body>
     
<div id="refresh"> 
</div>
        
<script>
//When the document is loaded
document.addEventListener("load", myTimer);
//subsequent calls
setInterval(myTimer, 1000);
function myTimer(){
  var d = new Date();
  document.getElementById("refresh").innerHTML = d.toLocaleString();
}
</script>   

</body>
</html>
Asif
  • 166
  • 9