vendredi 29 août 2014

How to globally get an HTML element once and use it multiple times?


Vote count:

0




I don't want to invoke



var element = document.getElementById('ph');


every time the UpdateClock get called for the sake of efficiency. So I attempted to put it globally rather than putting it inside UpdateClock. However, it does not work.



<!DOCTYPE html>
<html xmlns="http://ift.tt/lH0Osb">
<head>
<title>This is a title</title>
<script>
var element = document.getElementById('ph');

function UpdateClock() {
var now = new Date();
var hours = now.getHours();
var minutes = now.getMinutes();
var seconds = now.getSeconds();

if (hours < 10)
hours = "0" + hours;
if (minutes < 10)
minutes = "0" + minutes;
if (seconds < 10)
seconds = "0" + seconds;
element.innerHTML = hours + ":" + minutes + ":" + seconds;
}

</script>

</head>
<body onload="setInterval('UpdateClock()', 1000)">
<h1 id="ph"></h1>
</body>
</html>


What is wrong? Is there anything I am missing here?



asked 29 secs ago







How to globally get an HTML element once and use it multiple times?

Aucun commentaire:

Enregistrer un commentaire