Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    Regular Coder
    Join Date
    Dec 2009
    Posts
    173
    Thanks
    24
    Thanked 1 Time in 1 Post

    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!

  • #2
    Senior Coder
    Join Date
    Dec 2010
    Posts
    2,398
    Thanks
    12
    Thanked 570 Times in 563 Posts
    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);
    	});

  • #3
    Regular Coder
    Join Date
    Dec 2009
    Posts
    173
    Thanks
    24
    Thanked 1 Time in 1 Post
    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.

  • #4
    Senior Coder
    Join Date
    Dec 2010
    Posts
    2,398
    Thanks
    12
    Thanked 570 Times in 563 Posts
    You can't stop the first interval because you are losing the reference to it by overwriting it with the second setInterval()


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •