Link to home
Avatar of alcani
alcani

asked on

Javascript timer

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,
Avatar of Kyle Hamilton
Kyle Hamilton
Flag of United States of America image

look into setInterval
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.
Avatar of alcani
alcani

ASKER

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?
ASKER CERTIFIED SOLUTION
Avatar of Kyle Hamilton
Kyle Hamilton
Flag of United States of America image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial