0

I have an simple html document that should load the javascript again in intervall of 1000

so at the end of the document i added:

<script>
window.onload = function() {
 refreshTime(); }
</script> 

This functions is defined in the js document and goes like this:

function refreshTime() {

var a = new Date(); // Now
    var ahours = a.getHours();
    ........

and ends with:

   }
     refreshTime();
     setInterval(refreshTime, 1000);

so that the refreshTime() is out of the function refresh Time() because of the } If you understand what i mean?

So my problem is that this interval does not function, do i have to set the Interval in the Html document? Or at another place?

Em Sta
  • 1,676
  • 9
  • 29
  • 43
  • What you've shown looks like it should work. Please specify "does not function" and check the console for errors. – John Dvorak Mar 08 '13 at 16:47
  • "does not function" is too vague... Is refreshTime() not firing or are you not getting the expected result when calling it? – adamb Mar 08 '13 at 16:49
  • the refresh Time is somehow not firing! Sorry for my englisch! – Em Sta Mar 08 '13 at 16:50
  • i mean is it sufficent to only ad the refreshTime() at the end of the document without any function? – Em Sta Mar 08 '13 at 16:51
  • 1
    and with this: setTimeout('refreshTime()', 1000); ??? – Hackerman Mar 08 '13 at 16:52
  • 1
    This post may help. http://stackoverflow.com/questions/8779845/javascript-setinterval-not-working – o2bjang Mar 08 '13 at 16:56
  • the thing what is a little bit confussing for me is that when i put the hole code in a in the html document it does work! Can somebody explain me why? – Em Sta Mar 08 '13 at 17:00
  • 1
    @RobertRozas, that is not the proper way to invoke a function from within setTimeout. Em Sta has actually got that part right. – Derek Henderson Mar 08 '13 at 17:01
  • 1
    It is more reliable to use `setTimeout(code,millisec,lang)`, because of timing problems. Loops can overlap. Better to call once the function and at the end, call `setTimeOut` again – bgusach Mar 08 '13 at 17:11

1 Answers1

1

I suspect you have an error within refreshTime that is causing fail. I ran the following and it worked just fine:

function refreshTime() {
    console.log('Time refreshed!');
}
refreshTime();
setInterval(refreshTime, 1000);

So, the problem isn't with how you call refreshTime from the setInterval but rather something in the code you haven't shown us.

Derek Henderson
  • 9,388
  • 4
  • 42
  • 71
  • You have to be right! I mean the refreshTime refreshTime() works! There must be something between! Tomorrow i will talk with my colleague! But so far thanks! – Em Sta Mar 08 '13 at 17:10