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 10 of 10
  1. #1
    New to the CF scene
    Join Date
    Jan 2014
    Posts
    6
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Question How to stop a function from another function

    hi guys,
    Can anyone help me to figure out how can I stop working of a function from another function?

    I have a function that does a loop and when the thumbnail is clicked the loop has to stop.

    Here is the first function, it loops the images:

    Code:
    function loopy(){
    
              setTimeout(function(){
    	document.getElementById("main_image").src="images/big_main.jpg";
    		}, 500);
    
    		setTimeout(function(){
    		document.getElementById("main_image").src="images/big_1.jpg";
    		}, 1500);
    
    		setTimeout(function(){
    		document.getElementById("main_image").src="images/big_2.jpg";
    		}, 2500);
    
    		setTimeout(function(){
    		document.getElementById("main_image").src="images/big_3.jpg";
    		}, 3500);
    
    
    
    var myVar = setInterval(function(){
    
                   setTimeout(function(){
                 document.getElementById("main_image").src="images/big_main.jpg";
    		}, 500);
    
    		setTimeout(function(){
    		document.getElementById("main_image").src="images/big_1.jpg";
    		}, 1500);
    
    		setTimeout(function(){
    		document.getElementById("main_image").src="images/big_2.jpg";
    		}, 2500);
    
    		setTimeout(function(){
    		document.getElementById("main_image").src="images/big_3.jpg";
    		}, 3500);
    
    	}, 4500);
    }
    Here is the second function, it supposed to stop the loopy().\

    Code:
    function changeOne()
    
    	{
    	document.getElementById("main_image").src="images/big_1.jpg";
    	}

  • #2
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,155
    Thanks
    75
    Thanked 4,338 Times in 4,304 Posts
    I can't make any real sense of that code.

    See if this works:
    Code:
    var theLoop = null;
    var loopImages = ["big_main.jpg","big_1.jpg","big_2.jpg","big3_jpg"];
    var whichImage = 0;
    function loopy( ) 
    {
        document.getElementById("main_image").src="images/" + loopImages[whichImage];
        whichImage = ( whichImage + 1 ) % loopImages.length;
        theLoop = setTimeout( loopy, 1000 );
    }
    
    function startLoopy( )
    {
        whichImage = 0;
        theLoop = setTimeout( loopy, 500 );
    }
    
    function changeOne( )
    {
        clearTimeout( theLoop );
        document.getElementById("main_image").src="images/big_1.jpg";
    }
    Call startLoopy() to get things started, of course.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • Users who have thanked Old Pedant for this post:

    K555 (01-03-2014)

  • #3
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,610
    Thanks
    0
    Thanked 645 Times in 635 Posts
    Alternatively to get slightly more regular timing and use slightly less code:

    Code:
    var theLoop = null;
    var loopImages = ["big_main.jpg","big_1.jpg","big_2.jpg","big3_jpg"];
    var whichImage = 0;
    function loopy( ) 
    {
        document.getElementById("main_image").src="images/" + loopImages[whichImage];
        whichImage = ( whichImage + 1 ) % loopImages.length;
    }
    
    function startLoopy( )
    {
        whichImage = 0;
        theLoop = setInterval( loopy, 1000 );
    }
    
    function changeOne( )
    {
        clearInterval( theLoop );
        document.getElementById("main_image").src="images/big_1.jpg";
    }
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  • #4
    New to the CF scene
    Join Date
    Jan 2014
    Posts
    6
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Thank you so much, now it makes sense.

  • #5
    New to the CF scene
    Join Date
    Jan 2014
    Posts
    6
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Thank you very much.

  • #6
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,155
    Thanks
    75
    Thanked 4,338 Times in 4,304 Posts
    Felgall: I was carefully copying his code, where the first image changed after only half a second and then one second intervals thereafter. You are probably right that there was no reason to do that.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #7
    New to the CF scene
    Join Date
    Jan 2014
    Posts
    6
    Thanks
    2
    Thanked 0 Times in 0 Posts
    felgall, it doesn't really work though.. but thanks a lot.

  • #8
    New to the CF scene
    Join Date
    Jan 2014
    Posts
    6
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Old Pedant, your code works fine, a big thank you.

  • #9
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,610
    Thanks
    0
    Thanked 645 Times in 635 Posts
    Quote Originally Posted by K555 View Post
    felgall, it doesn't really work though.. but thanks a lot.
    Don't know why you say that Old Pedant's version works and mine doesn't. Except for the half second difference at the start they are identical.

    For processes that repeat you should use setInterval as setTimeout is intended for delays and is less efficient when you call it multiple times as compared to calling setInterval once.

    To make the code exactly identical but using the more efficient calls you'd use:

    Code:
    var theLoop = null;
    var loopImages = ["big_main.jpg","big_1.jpg","big_2.jpg","big3_jpg"];
    var whichImage = 0;
    function loopy( ) 
    {
        document.getElementById("main_image").src="images/" + loopImages[whichImage];
        whichImage = ( whichImage + 1 ) % loopImages.length;
    }
    
    function startLoopy( )
    {
        whichImage = 0;
        setTimeout(function() {theLoop = setInterval( loopy, 1000 );loopy();},500);
    }
    
    function changeOne( )
    {
        clearInterval( theLoop );
        document.getElementById("main_image").src="images/big_1.jpg";
    }
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  • Users who have thanked felgall for this post:

    K555 (01-04-2014)

  • #10
    New to the CF scene
    Join Date
    Jan 2014
    Posts
    6
    Thanks
    2
    Thanked 0 Times in 0 Posts
    felgall, the previous code wasn't stopping the loop when was clicking on the thumbnail, for some reason.. may be I didn't copy your code properly.. But now it does, thank you very much for your respond.


  •  

    Posting Permissions

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