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>

Recommended Answers

All 9 Replies

Try to make the time_ref2 =... as a global variable, you can do this by simply removing the var keyword right before it.

Taking Essential's suggestion one very small step further, you can avoid using a global variable by tucking the setInterval reference away somewhere other than the global namespace - for example as an attribute of the "time_now" document element, like this;

$(document).ready(function(){
	//Paleidimo funkcija
	var time_ref = setTimeout(function(){
		$('#time_now').attr({
			t_ref : setInterval(function(){
				$('#time_now').load('time.php?id='+ Math.random());
			}, 1000)
		});
	}, 3000);
	//Laiko sustabdymo funkcija
	$("#stop").click(function(){
		clearInterval($('#time_now').attr('t_ref'));
	});
});

I am slightly surprised that it works, but it does!

Airshow

Doh! I've just realised that you can rely on closure of the outer function and simply declare time_ref2 there.

$(document).ready(function(){
	var time_ref2;
	//Paleidimo funkcija
	var time_ref = setTimeout(function(){
		time_ref2 = setInterval(function(){
			$('#time_now').load('time.php?id='+ Math.random());
		}, 1000);
	}, 3000);
	//Laiko sustabdymo funkcija
	$("#stop").click(function(){
		clearInterval(time_ref2);
	});
});

I still like the solution in my post above but why make it more complex than it needs to be?

Airshow

Declaring 1 global variable wil not hurt your program, and you can skip alot of lines by doing it, instead of finding another way, just what have Airshow did.

thanks guys, a lot

I do not want to create another similar topic, ask it here.
How to activate jquery a function of the php code. For example

if($city != "Paris"){ run function on of jquery for example time }

Wher i can finde jquery for beginner info, samples? Now i searching in www.jquey.com, But maybe there are analogies?

Thanks for help

Lola^2,

Remember that php runs server-side, and javascript (including jquery) runs client-side.

As php controls the content of dynamic pages, it can control not only HTML but also javascript. The level of control exercised can be as simple or as complex as you want - for example, including/excluding chunks of code, or dynamically building statements.

Here is an outline example using your $city != "Paris" condition:

<script>
$(document).ready(function(){
	//..... statement block 1 .....
	<? if($city != "Paris"){ ?>
	//..... statement block 2 .....
	<? } ?>
	//..... statement block 3 .....
</script>

Airshow

The bad thing about relying things over those frameworks, is that they make you script dependent on the work that they have provided. Unlike if you are wel aware on the things of what JavaScript can offer.

All it does is simplifying the work on the script that you are implementing, on the fact that you are forgetting the most important (parts, methods and properties) of the JavaScript language.

Words of motivation:
- I always prefer to write my script, from scratch to finish using a plain text-editor in my PDA using OPERA Browser v8.65 for Nokia s60v2series on a resolution of 176pixel wide, and 201pixel down:LCD screen.

Running and debbugging my code using a native alert function on every critical block so that i can easily pinpoint my errors'.

But that wont prevent me from writing and improving my JavaScript skills...

But if you really need their tutorial, im sure you can find lots of it when you google around.

essential

Airshow is right.
Don't use Global variable,unless there is a real need.
Global variable is evil.

@hengzhe,

Not if you know how to handle it, and im sure that you are not stupid enough on commiting to those mistakes. As i have said, one global instance of variable, won't affect everything in your work, especially on a small bits of codes like what have lolalola posted. Please don't make things more complicated than it is right now. If you have other ways--then post it intead of talking and mocking my post. And one more thing, i think you should stop injecting your pay-up site in here, coz' it's quite irritating...

Prove your things with your own codes not by your words...

essential

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.