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 cineweekly.com's Avatar
    Join Date
    Aug 2010
    Posts
    485
    Thanks
    14
    Thanked 3 Times in 3 Posts

    Amateur question about combining functions

    I'm not familiar with Javascript yet and I'm trying to combine these (functions?) to reduce space. Not sure of the syntax.

    Code:
    <script type="text/javascript">
    $(document).ready(function() {
        $('.slideshowA').cycle({
    		fx: 'fade',
    		speed: 4000,
    		delay: 0
    	});
    });
    </script> 
    <script type="text/javascript">
    $(document).ready(function() {
        $('.slideshowB').cycle({
    		fx: 'fade',
    		speed: 4000,
    		delay: -1000
    	});
    });
    </script>
    <script type="text/javascript">
    $(document).ready(function() {
        $('.slideshowC').cycle({
    		fx: 'fade',
    		speed: 4000,
    		delay: -2000
    	});
    });
    </script>

  • #2
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,020
    Thanks
    75
    Thanked 4,323 Times in 4,289 Posts
    Well, first of all, you aren't really using basic JavaScript when you do that.

    You are using the jQuery library. So some of the code is going to be a bit (or a LOT!) different than if you were using ordinary JavaScript.

    It is kind of like you are using a GPS navigation system (e.g, a TomTom or similar) but never learned how to read a map. You are trusting that your navigation system will always know how to read the map for you.

    But to directly answer your question:
    Code:
    <script type="text/javascript">
    $(document).ready(function() {
        $('.slideshowA').cycle({
    		fx: 'fade',
    		speed: 4000,
    		delay: 0
    	});
        $('.slideshowB').cycle({
    		fx: 'fade',
    		speed: 4000,
    		delay: -1000
    	});
        $('.slideshowC').cycle({
    		fx: 'fade',
    		speed: 4000,
    		delay: -2000
    	});
    });
    </script>
    I don't think you an make it much shorter than that without major changes to how you code it.
    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.

  • #3
    Regular Coder cineweekly.com's Avatar
    Join Date
    Aug 2010
    Posts
    485
    Thanks
    14
    Thanked 3 Times in 3 Posts
    That's what I was looking for, thanks! I was so close when playing around but couldn't nail it down. Now I know.

  • #4
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,020
    Thanks
    75
    Thanked 4,323 Times in 4,289 Posts
    Just as a for-instance, you could probable rewrite it all thus:
    Code:
    <script type="text/javascript">
    function doCycle( which, wait )
    {
        $(".slideshow" + which).cycle({
    		fx: 'fade',
    		speed: 4000,
    		delay: wait
    	});
    }
    $(document).ready(
        function() 
        {
            doCycle("A",0);
            doCycle("B",-1000);
            doCycle("C",-2000);
        }
    );
    </script>
    Shorter, but hardly worth doing the rewrite, is it?
    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.


  •  

    Posting Permissions

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