Go Back   CodingForums.com > :: Client side development > JavaScript programming > JavaScript frameworks

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 12-12-2011, 02:23 AM   PM User | #1
sorlaker
Regular Coder

 
Join Date: Dec 2009
Posts: 166
Thanks: 23
Thanked 1 Time in 1 Post
sorlaker has a little shameless behaviour in the past
Why this isnt working?

Code:
<script>
	$("#imovel1").mouseenter(function(){
		var rel = 1;
		var changer = setInterval(function(){
			for(i = 1; i <= 5; i++){
					$(this).find('img[relimg='+i+']').hide();
			}
			rel = rel + 1;
			if (rel > 5) { rel = 1; }
			$(this).find('img[relimg='+rel+']').show();
		}, 1500);
	}).mouseleave(function(){
		clearInterval(changer);
	});
</script>
Cant figute out why this code isnt working.
Appreciate if someone could help me.


PS: The mouseenter and mouseleave is working. But the setInterval isnt. Thanks!
sorlaker is offline   Reply With Quote
Old 12-12-2011, 07:11 AM   PM User | #2
devnull69
Senior Coder

 
Join Date: Dec 2010
Posts: 2,245
Thanks: 10
Thanked 531 Times in 525 Posts
devnull69 will become famous soon enough
With the anonymous function inside the setTimeout you are changing the scope, so "this" will no longer reference the element #imovel1 but rather the global "window" object.

Try this
Code:
	$("#imovel1").mouseenter(function(){
		var rel = 1;
		var that = this;
		var changer = setInterval(function(){
			for(i = 1; i <= 5; i++){
					$(that).find('img[relimg='+i+']').hide();
			}
			rel = rel + 1;
			if (rel > 5) { rel = 1; }
			$(that).find('img[relimg='+rel+']').show();
		}, 1500);
	}).mouseleave(function(){
		clearInterval(changer);
	});
devnull69 is offline   Reply With Quote
Old 12-12-2011, 10:22 AM   PM User | #3
sorlaker
Regular Coder

 
Join Date: Dec 2009
Posts: 166
Thanks: 23
Thanked 1 Time in 1 Post
sorlaker has a little shameless behaviour in the past
Yeah sorry about that. Forgot to change that part.

Code:
	var rel = 1;
	var changer = setInterval(function(){
		for(i = 1; i <= 5; i++){
					$('#imovel1').find('img[relimg='+i+']').hide();
			}
			rel = rel + 1;
			if (rel > 5) { rel = 1; }
			$('#imovel1').find('img[relimg='+rel+']').show();
	}, 1500);
	$("#imovel1").mouseenter(function(){
		changer = setInterval(function(){
		for(i = 1; i <= 5; i++){
					$(this).find('img[relimg='+i+']').hide();
			}
			rel = rel + 1;
			if (rel > 5) { rel = 1; }
			$(this).find('img[relimg='+rel+']').show();
	}, 1500);
	}).mouseleave(function(){
		for(i = 1; i <= 5; i++){
					$(this).find('img[relimg='+i+']').hide();
			}
			$(this).find('img[relimg=1]').show();
		clearInterval(changer);
	});
This is the code. I can't stop the bloody setInterval! Help me!

Last edited by sorlaker; 12-12-2011 at 02:37 PM..
sorlaker is offline   Reply With Quote
Old 12-12-2011, 07:37 PM   PM User | #4
devnull69
Senior Coder

 
Join Date: Dec 2010
Posts: 2,245
Thanks: 10
Thanked 531 Times in 525 Posts
devnull69 will become famous soon enough
You can't stop the first interval because you are losing the reference to it by overwriting it with the second setInterval()
devnull69 is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 01:36 AM.


Advertisement
Log in to turn off these ads.