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

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-03-2012, 11:48 PM   PM User | #1
cineweekly.com
Regular Coder

 
cineweekly.com's Avatar
 
Join Date: Aug 2010
Posts: 485
Thanks: 14
Thanked 3 Times in 3 Posts
cineweekly.com can only hope to improve
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>
__________________
¿¿¿ curious ???
cineweekly.com is offline   Reply With Quote
Old 12-03-2012, 11:54 PM   PM User | #2
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,579
Thanks: 62
Thanked 4,064 Times in 4,033 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
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.
Old Pedant is offline   Reply With Quote
Old 12-03-2012, 11:57 PM   PM User | #3
cineweekly.com
Regular Coder

 
cineweekly.com's Avatar
 
Join Date: Aug 2010
Posts: 485
Thanks: 14
Thanked 3 Times in 3 Posts
cineweekly.com can only hope to improve
That's what I was looking for, thanks! I was so close when playing around but couldn't nail it down. Now I know.
__________________
¿¿¿ curious ???
cineweekly.com is offline   Reply With Quote
Old 12-03-2012, 11:59 PM   PM User | #4
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,579
Thanks: 62
Thanked 4,064 Times in 4,033 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
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.
Old Pedant 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 10:52 AM.


Advertisement
Log in to turn off these ads.