Note that there are some explanatory texts on larger screens.

plurals
  1. POsetInterval not working with specific function
    primarykey
    data
    text
    <p>I have a script that creates a bar at the top right of the browser with current date and time. I'm trying to update it every thirty seconds so that it keeps a live time, and not just the time when the page loaded. You'll see that I create the time bar with the set_time() function onload, and then create a setInterval that is intended to call timer() which in turn updates the time by calling the set_time() function again. The timer is clearly firing because every 4 seconds I get an alert box with "hi" (from the timer() function), but then when it's supposed to call set_time() after this it isn't working. I should be getting an alert with "set time" (located at the end of the set_time() function) each time the timer is fired as well, which I do not. Can anybody help?</p> <pre><code>window.onload = function(){ set_time(); window.setInterval(timer, 4000); }; function timer(){ alert('hi'); set_time(); } function set_time(){ //create date object to manipulate var d = new Date(); //current day of the week var d_names= new Array("Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday"); curr_day = d.getDay(); //current date var m_names = new Array("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"); var curr_date = d.getDate(); var curr_month = d.getMonth(); var curr_year = d.getFullYear(); //current Time var a_p = ""; var curr_hour = d.getHours(); if (curr_hour &lt; 12) { a_p = "a.m."; } else { a_p = "p.m."; } if (curr_hour == 0) { curr_hour = 12; } if (curr_hour &gt; 12) { curr_hour = curr_hour - 12; } var curr_min = d.getMinutes(); curr_min = curr_min + ""; if (curr_min.length == 1) { curr_min = "0" + curr_min; } element = document.getElementById('dateTime'); if(element){ //if dateDiv already exists, update contents dateDiv.innerHTML = ''; dateDiv.innerHTML = '&lt;p&gt;' + d_names[curr_day] + ', ' + m_names[curr_month] + ' ' + curr_date + ', ' + curr_year + ' | ' + '&lt;strong&gt;' + curr_hour + ':' + curr_min + ' ' + a_p + '&lt;/strong&gt;' + '&lt;/p&gt;'; } else{ //else create new dateDiv and append it to DOM body var dateDiv = document.createElement('div'); dateDiv.innerHTML = '&lt;p&gt;' + d_names[curr_day] + ', ' + m_names[curr_month] + ' ' + curr_date + ', ' + curr_year + ' | ' + '&lt;strong&gt;' + curr_hour + ':' + curr_min + ' ' + a_p + '&lt;/strong&gt;' + '&lt;/p&gt;'; dateDiv.id = 'dateTime'; document.body.appendChild(dateDiv); } alert('set time'); //setTimeout("set_time()", 3000); } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload