Javascript timer

alcani
alcani used Ask the Experts™
on
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,
Comment
Watch Question

Do more with

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

Commented:
look into setInterval
Roopesh ReddyIT Analyst
Top Expert 2012

Commented:
Top Expert 2007

Commented:
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:
<html>
<body>
<input type="text" id="clock" />
<script type="text/javascript">
var int=window.setInterval(
function(){
var d=new Date();
var t=d.toLocaleTimeString();
document.getElementById("clock").value=t;},1000);
</script>
<button onclick="int=window.clearInterval(int)">Stop</button>
</body>
</html>

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.

Author

Commented:
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
Commented:
If "Submit" is a javascript function then it's very easy:

var int;

int = setInterval(){
   submit();
}, 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