...

View Full Version : Amateur question about combining functions



cineweekly.com
12-04-2012, 12:48 AM
I'm not familiar with Javascript yet and I'm trying to combine these (functions?) to reduce space. Not sure of the syntax.


<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>

Old Pedant
12-04-2012, 12:54 AM
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:


<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.

cineweekly.com
12-04-2012, 12:57 AM
That's what I was looking for, thanks! I was so close when playing around but couldn't nail it down. Now I know.

Old Pedant
12-04-2012, 12:59 AM
Just as a for-instance, you could probable rewrite it all thus:


<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?



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum