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-06-2012, 08:35 PM   PM User | #1
sinsysonline
New Coder

 
Join Date: Dec 2012
Posts: 15
Thanks: 4
Thanked 0 Times in 0 Posts
sinsysonline is an unknown quantity at this point
Post Merging Two Scripts on onInterval infinite loop

Greetings All,

I've been having trouble combining 2 scripts and to have them automatically preform them both at the same time.

I have one script that is a simple slideshow that loads dynamic text into a DIV every 5 seconds.

The next script edits the SPAN of the text to resize according to the character amount. The text it pulls can be any length and it simply edits the font size to force the post to fill the entire static box.

How can I get this resizing script to run in parallel right after the slide switches? Should I combine them into one script? Or should I just also set it to an interval of 5 seconds (and how would I do that?)

Script 1 - Slideshow:


[CODE]
<script>
$("#slideshow > div:gt(0)").hide();

setInterval(function() {
$('#slideshow > div:first')
.fadeOut(1000)
.next()
.fadeIn(1000)
.end()
.appendTo('#slideshow');
}, 5000);
</script>
[CODE]

Script 2 - Resize Text

[CODE]
<script>
$(function resize() {
$('#slideshow span').css('font-size', '4em');

while( $('#slideshow span').height() > $('#slideshow').height() ) {
$('#slideshow span').css('font-size', (parseInt($('#slideshow span').css('font-size')) - 1) + "px" );
}
});
</script>
[CODE]
sinsysonline is offline   Reply With Quote
Old 12-06-2012, 08:40 PM   PM User | #2
felgall
Master Coder

 
felgall's Avatar
 
Join Date: Sep 2005
Location: Sydney, Australia
Posts: 5,449
Thanks: 0
Thanked 496 Times in 488 Posts
felgall is a jewel in the roughfelgall is a jewel in the roughfelgall is a jewel in the rough
If you want them both to update one immediately after the other then it would be best to place them inside the same setInterval:

Code:
$("#slideshow > div:gt(0)").hide();

setInterval(function() {
$('#slideshow > div:first')
.fadeOut(1000)
.next()
.fadeIn(1000)
.end()
.appendTo('#slideshow');

$(function resize() {
$('#slideshow span').css('font-size', '4em');

while( $('#slideshow span').height() > $('#slideshow').height() ) {
$('#slideshow span').css('font-size', (parseInt($('#slideshow span').css('font-size')) - 1) + "px" );
}
});
}, 5000);
__________________
Stephen
Learn Modern JavaScript - http://javascriptexample.net/
Helping others to solve their computer problem at http://www.felgall.com/
felgall is offline   Reply With Quote
Old 12-06-2012, 08:52 PM   PM User | #3
sinsysonline
New Coder

 
Join Date: Dec 2012
Posts: 15
Thanks: 4
Thanked 0 Times in 0 Posts
sinsysonline is an unknown quantity at this point
THANK YOU! That worked beautifully.

The only thing it seemed to break is loading on page.

I'm trying to do my research but I'm having trouble.

How could I call it to go immediately on page load AND interval every 5 seconds?
sinsysonline is offline   Reply With Quote
Old 12-06-2012, 09:02 PM   PM User | #4
sinsysonline
New Coder

 
Join Date: Dec 2012
Posts: 15
Thanks: 4
Thanked 0 Times in 0 Posts
sinsysonline is an unknown quantity at this point
Sorry, let me clarify.

It doesn't load "onLoad" of the webpage and takes 5 seconds to put itself into effect.
sinsysonline is offline   Reply With Quote
Old 12-06-2012, 09:25 PM   PM User | #5
felgall
Master Coder

 
felgall's Avatar
 
Join Date: Sep 2005
Location: Sydney, Australia
Posts: 5,449
Thanks: 0
Thanked 496 Times in 488 Posts
felgall is a jewel in the roughfelgall is a jewel in the roughfelgall is a jewel in the rough
To run the code immediately as well as every 5 seconds we'll need to pull the function out of the setInterval so we can call it immediately as well as from inside the setInterval - like this:

Code:
$("#slideshow > div:gt(0)").hide();

var sld = function() {
$('#slideshow > div:first')
.fadeOut(1000)
.next()
.fadeIn(1000)
.end()
.appendTo('#slideshow');

$(function resize() {
$('#slideshow span').css('font-size', '4em');

while( $('#slideshow span').height() > $('#slideshow').height() ) {
$('#slideshow span').css('font-size', (parseInt($('#slideshow span').css('font-size')) - 1) + "px" );
}
});
}
setInterval(sld, 5000);
sld();
__________________
Stephen
Learn Modern JavaScript - http://javascriptexample.net/
Helping others to solve their computer problem at http://www.felgall.com/
felgall is offline   Reply With Quote
Users who have thanked felgall for this post:
sinsysonline (12-06-2012)
Old 12-06-2012, 09:46 PM   PM User | #6
sinsysonline
New Coder

 
Join Date: Dec 2012
Posts: 15
Thanks: 4
Thanked 0 Times in 0 Posts
sinsysonline is an unknown quantity at this point
Thanks so much for the help! I tried that but figured there may be an easier way.

Amazingly fast response time! I really appreciate everything you've done.
sinsysonline 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 06:04 AM.


Advertisement
Log in to turn off these ads.