Javascript timer

alcani used Ask the Experts™
Hi experts,

I’m looking for a simple way to set a JavaScript timer to pull content from the server session every few minutes to keep the session active only while the browser window remains open.

Thanks for your comments,
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Kyle HamiltonData Scientist
Most Valuable Expert 2014

look into setInterval
Roopesh ReddyIT Analyst
Top Expert 2012

Top Expert 2007

The example at w3schools isn't what I'd use. They pass a String reference to a function in the setInterval call - I'd do it like this:
<input type="text" id="clock" />
<script type="text/javascript">
var int=window.setInterval(
var d=new Date();
var t=d.toLocaleTimeString();
<button onclick="int=window.clearInterval(int)">Stop</button>

Open in new window

Why, you ask? Because passing a String of code - or function name - to either setTimeout or setInterval is tantamount to using the evil eval statement.


Thanks for your comments,

In fact I would like to every 9 minutes execute a function call “Submit”

Could you help me with the syntax?
Data Scientist
Most Valuable Expert 2014
If "Submit" is a javascript function then it's very easy:

var int;

int = setInterval(){
}, 540000); // 9mins = 1milisec*1000*9*60

function submit(){
    // your function code

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial