1

I developed a javascript function that shows me the current time. Until then I had no problems. The problem is that I can not call this function in the place I want ... how can I call it using this?

<script language="JavaScript">

function showTime() {
  var timeNow = new Date();
  var hours   = timeNow.getHours();
  var minutes = timeNow.getMinutes();
  var seconds = timeNow.getSeconds();
  var timeString = "" + ((hours > 24) ? hours - 12 : hours);
  timeString  += ((minutes < 10) ? ":0" : ":") + minutes;
  timeString  += ((seconds < 10) ? ":0" : ":") + seconds;
  timeString  += (hours >= 12) ? " P.M." : " A.M.";
  document.htmlClock.timeField.value = timeString;
  timerID = setTimeout("showTime()", 1000);
}

</script>

Location where I want to call the function

<li><a href="#"><i class="fa fa-clock-o"></i>Hours here</a></li>
2
  • simply call the function after having declared it.. or use an interval or timeout outside the function like in jo_va's answer Commented Feb 8, 2019 at 16:05
  • Define Automatically. What do you mean by that? When the page loads or based on a frequency or when the user clicks on something or.... Commented Feb 8, 2019 at 16:06

7 Answers 7

1

You're suddenly calling the function showTime, and passing implicitly undefined to the function setTimeout. In your approach you're actually passing a String, which will be evaluated (BAD).

I recommend you to use the function setInterval for repeated calls of a function.

Likewise, use the event DOMContentLoaded to be sure the HTML DOM tree is totally loaded before any DOM manipulation.

function showTime() {
  var timeNow = new Date();
  var hours = timeNow.getHours();
  var minutes = timeNow.getMinutes();
  var seconds = timeNow.getSeconds();
  var timeString = "" + ((hours > 24) ? hours - 12 : hours);
  timeString += ((minutes < 10) ? ":0" : ":") + minutes;
  timeString += ((seconds < 10) ? ":0" : ":") + seconds;
  timeString += (hours >= 12) ? " P.M." : " A.M.";
  document.getElementById('hourshere').textContent = timeString;
}

document.addEventListener("DOMContentLoaded", function(event) {
  showTime();
  setInterval(showTime, 1000);
});
Location where I want to call the function
<li><a href="#"><i class="fa fa-clock-o"></i>
<span id='hourshere'>Hours here</span>
</a></li>

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for your response!
0

Place a <span> with an id where you want the time to be shown. Then query that <span> by id and set its textContent. Use the setInterval function if you want to update the time at a regular interval:

function getTime() {
  const timeNow = new Date();
  const hours   = timeNow.getHours();
  const minutes = timeNow.getMinutes();
  const seconds = timeNow.getSeconds();
  let timeString = '' + ((hours > 24) ? hours - 12 : hours);
  timeString  += ((minutes < 10) ? ":0" : ":") + minutes;
  timeString  += ((seconds < 10) ? ":0" : ":") + seconds;
  timeString  += (hours >= 12) ? " P.M." : " A.M.";
  return timeString;
}

const hoursSpan = document.getElementById('hours');
hoursSpan.textContent = getTime();

setInterval(() => {
  hoursSpan.textContent = getTime();
}, 1000);
<li>
  <a href="#">
    <i class="fa fa-clock-o"></i><span id="hours"></span>
  </a>
</li>

1 Comment

Perfect! Thanks everyone for the answer !!
0
<script language="JavaScript">

function showTime() {
  var timeNow = new Date();
  var hours   = timeNow.getHours();
  var minutes = timeNow.getMinutes();
  var seconds = timeNow.getSeconds();
  var timeString = "" + ((hours > 24) ? hours - 12 : hours);
  timeString  += ((minutes < 10) ? ":0" : ":") + minutes;
  timeString  += ((seconds < 10) ? ":0" : ":") + seconds;
  timeString  += (hours >= 12) ? " P.M." : " A.M.";
  document.htmlClock.timeField.value = timeString;
  //timerID = setTimeout("showTime()", 1000); //No longer needed
}

</script>

So it would be:

var t=setInterval(showTime,1000);

And to stop it, you can run:

clearInterval(t);

1 Comment

Thanks for your response!
0
<li><a href="#"><i class="fa fa-clock-o"></i><script>showTime()</script></a></li>

1 Comment

Thanks for your response!
0

Add onclick="showTime() to the html element you want when clicked, calls the function showTime() .

If you want to show the time after the link is clicked,

<li><a href="#" onclick="showTime()"><i class="fa fa-clock-o"></i>Hours here</a></li>

or if you want to show the time after the icon is clicked,

<li><a href="#"><i class="fa fa-clock-o" onclick="showTime()"></i>Hours here</a></li>

2 Comments

Thanks for your response!
Happy to help :D
0

On the javascript side maybe you can do something like this:

document.getElementById("clock").onclick = {
var timeNow = new Date();
var hours   = timeNow.getHours();
var minutes = timeNow.getMinutes();
var seconds = timeNow.getSeconds();
var timeString = "" + ((hours > 24) ? hours - 12 : hours);
timeString  += ((minutes < 10) ? ":0" : ":") + minutes;
timeString  += ((seconds < 10) ? ":0" : ":") + seconds;
timeString  += (hours >= 12) ? " P.M." : " A.M.";
document.htmlClock.timeField.value = timeString;
timerID = setTimeout("showTime()", 1000);
};

Html

<li><a href="#"><i id="clock"></i>Hours here</a></li>

Comments

0

The accepted answer and others are all great. My only suggestion is to modify your original code for the date. As I was looking at it, I noticed some parts could be left out.

Here's the JS from the accepted answer:

function getTime() {
  const timeNow = new Date();
  const hours   = timeNow.getHours();
  const minutes = timeNow.getMinutes();
  const seconds = timeNow.getSeconds();
  let timeString = '' + ((hours > 24) ? hours - 12 : hours);
  timeString  += ((minutes < 10) ? ":0" : ":") + minutes;
  timeString  += ((seconds < 10) ? ":0" : ":") + seconds;
  timeString  += (hours >= 12) ? " P.M." : " A.M.";
  return timeString;
}

const hoursSpan = document.getElementById('hours');
hoursSpan.textContent = getTime();

setInterval(() => {
  hoursSpan.textContent = getTime();
}, 1000);

And here is what I suggest:

function getTime() {
  const timeNow = new Date();
  const hours   = timeNow.getHours();
  const minutes = timeNow.getMinutes();
  const seconds = timeNow.getSeconds();
  // use hours > 12 to generate 12-hour time because hours will never be greater than 24
  let timeString = '' + ((hours > **12**) ? hours - 12 : hours);
  timeString  += ((minutes < 10) ? ":0" : ":") + minutes;
  timeString  += ((seconds < 10) ? ":0" : ":") + seconds;
  timeString  += (hours >= 12) ? " P.M." : " A.M.";
  return timeString;
}

const hoursSpan = document.getElementById('hours');
// can omit this line, since it is handled by the setInterval function
// hoursSpan.textContent = getTime();

setInterval(() => {
  hoursSpan.textContent = getTime();
}, 1000);

The markup is unchanged:

<li>
  <a href="#">
    <i class="fa fa-clock-o"></i><span id="hours"></span>
  </a>
</li>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.