Forum Moderators: open

Message Too Old, No Replies

run functions

         

Lolalola

1:13 pm on Jul 21, 2009 (gmt 0)



What's wrong? Why don't work stop funkction?
I think that the poor turn to the function, but otherwise I do not know ...

<html>
<head>
<script type="text/javascript" src="jquery.js"></script>
<script>
$(document).ready(function()
{
//Paleidimo funkcija
var time_ref = setTimeout(function()
{
var time_ref2 = setInterval(function()
{
$('#time_now').load('time.php?id='+ Math.random());
}, 1000);

}, 3000);

//Laiko sustabdymo funkcija
$("#stop").click(function()
{
clearInterval(time_ref2);
});

});
</script>
</head>
<body>
<div align="center" id="time_now">--:--:--</div>
<button id="stop">Stop</button>
</body>
</html>

whoisgregg

6:07 pm on Jul 22, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hello Lolalola,

I believe this is a variable scope problem. Basically, by putting "var " in front of the time_ref and time_ref2 variables inside that function, you are declaring those as local variables. Instead, remove the var from each. So those 3 lines would look like:

time_ref = setTimeout(function() 
{
time_ref2 = setInterval(function()