   [web] How to run a java script once every minute?
  I need to run a java script once every minute which will use ajax to execute a php script on my server to update the users online status. I know how to do everything else other than running a java script every minute, any idea how to do this?   
  My previous answer was way too vague (not to say "wrong" :( ). 2nd approach:Use the body's onLoad event handler to start a timer. E.g. soonLoad="window.setInterval('timedFunction();',1000);"so that the function timedFunction is invoked after 1000 milli-seconds.Then a &#106avascript functionfunction timedFunction() {    // do something    window.setInterval("timedFunction();",1000);}will perform its job and sets another timer for self to be invoked again 1000 milli-seconds later.   ;Quote:Original post by haegarrMy previous answer was way too vague (not to say "wrong" :( ). 2nd approach:Use the body's onLoad event handler to start a timer. E.g. soonLoad="window.setInterval('timedFunction();',1000);"so that the function timedFunction is invoked after 1000 milli-seconds.Then a &#106avascript functionfunction timedFunction() {    // do something    window.setInterval("timedFunction();",1000);}will perform its job and sets another timer for self to be invoked again 1000 milli-seconds later.Thank you, it works perfectly!   ;  SetInterval will execute the function once per minute.  The second call inside the function on the first call will add a second instance of the function call.  (so it will get called twice every minute.)  The third and fourth calls will add two more instances. Remove the setInterval from inside the function so you don't get an exponential progression of numbers of calls, increasing every minute.  (1, 2, 4, 8, ...)   ;Quote:Original post by ID MerlinSetInterval will execute the function once per minute.  The second call inside the function on the first call will add a second instance of the function call...Good point! It seems that setTimeout is a one-shot, while setInterval is continuous.   ;Quote:Original post by haegarr...window.setInterval("timedFunction();",1000);...'eval()' is evil (because of performance impact and possible code injection) and only needed in very rare circumstances.Don't use it if you don't have to!You are calling eval implicit every second in your example (because you gave setInterval an expression, not a function), which starts the &#106avascript parser (and JIT compiler) every second. A better example would be:window.setInterval(timedFunction,1000);And if you want to use parameters for the function call, than use a closure instead of another (evil) expression:window.setInterval(function() {timedFunction(parameter)},1000);;  You don't need an onload function. The browser will execute <script> tags as soon as it finds them (unless you're using deferred script functionality, but that's not something you need to worry about unless you explicitly requested deferred script execution). If you place your JS code at the bottom of the page, the browser will have constructed its DOM already and you can act on it as usual.Although a cleaner solution would involve hooking the onload event anyway (but NOT using the onload attribute. Using HTML tag attributes for event handling is considered bad practice). The common JS libraries like Prototype, jQuery, MooTools, YUI etc. provide proper onload event handling.   ;Quote:Original post by ID MerlinSetInterval will execute the function once per minute.  The second call inside the function on the first call will add a second instance of the function call.  (so it will get called twice every minute.)  The third and fourth calls will add two more instances. Remove the setInterval from inside the function so you don't get an exponential progression of numbers of calls, increasing every minute.  (1, 2, 4, 8, ...)I was wondering why my computer was running slow and kept crashing while I was working on my website! lol   