1
function clock()
{
var date = new Date();
var dateLocate = date.toLocaleString()
var paragraph = window.document.querySelector("#data");
paragraph.textContent = dateLocate;
window.setInterval(dateUpdate, 1000);
function dateUpdate()
{
var date = new Date();
var dateLocate = date.toLocaleString()
var paragraph = window.document.querySelector("#data");
paragraph.textContent = dateLocate;
}
}
window.addEventListener("load", clock);
<!doctype html>
<html lang="pt-br">
<head>
<meta charset="utf-8">
<title>clock</title>
</head>
<body>
<div id="clock">
<p id="data"></p>
</div>
</body>
</html>
The above code samples the same variables being declared. Well, if you take the same variables within the function clock
and leave it at that.
function clock()
{
window.setInterval(dateUpdate, 1000);
function dateUpdate()
{
var date = new Date();
var dateLocate = date.toLocaleString()
var paragraph = window.document.querySelector("#data");
paragraph.textContent = dateLocate;
}
}
window.addEventListener("load", clock);
The date will be updated automatically depending on your location, but when the page is updated the function dateUpdate
will be called after 1 second and will not call immediately, so if I remove the variables within the function dateUpdate
and leave as global in this way.
function clock()
{
var date = new Date();
var dateLocate = date.toLocaleString()
var paragraph = window.document.querySelector("#data");
window.setInterval(dateUpdate, 1000);
function dateUpdate()
{
paragraph.textContent = dateLocate;
}
}
window.addEventListener("load", clock);
The date appears, but is not updated in 1 second, so I would have to go back to the first code declaring the same variables. How I would make date to be sampled on the page of imediado and being updated taken 1 second without having to repeat the variables?