We're a community of 1077K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,076,451 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

jQuery Callback Problem

Hi,

I have a problem with jquery callback. The callback is executed before the fadeIn animation is completed. What im trying to do is, I want to replace an existing image with a new image and resize the new image to a specific size.

var filepath = "images/test.jpg";
$('#imagewrap').css('background-image', 'url("images/loader.gif")');//display a loading gif
$('#uimage').hide();//hide the current image
var i = $('<img />').attr('src', filepath).load(function()//load the new image 
{
    $('#uimage').attr('src', i.attr('src'));//change the src to new image
    $('#imagewrap').css('background-image', 'none');//hide the loading gif
    $('#uimage').fadeIn('slow', resize());//fade in the new image
}); 

function resize()
{
alert($('#uimage').width());//it shows the width of the previous image
}
3
Contributors
4
Replies
12 Hours
Discussion Span
1 Year Ago
Last Updated
5
Views
ShinyEdge
Newbie Poster
14 posts since Mar 2011
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

Have you tried this:

$('#uimage').fadeIn('slow', function(){ resize(); });

Are you sure there is no CSS for that id specifying a fixed width?

pritaeas
Posting Prodigy
Moderator
9,316 posts since Jul 2006
Reputation Points: 1,178
Solved Threads: 1,467
Skill Endorsements: 86

your code fixed it. thanks. whats wrong in calling the function directly?

ShinyEdge
Newbie Poster
14 posts since Mar 2011
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

You would want the following:

$('#uimage').fadeIn('slow', resize);

Adding the () at the end executes the function. So instead of fadeIn executing the function when it is done doing it's thing, you have it executing and returning undefined as the function to get executed (as resize returns undefined).

stbuchok
Practically a Posting Shark
876 posts since May 2011
Reputation Points: 138
Solved Threads: 124
Skill Endorsements: 2

thanks for the explanation

ShinyEdge
Newbie Poster
14 posts since Mar 2011
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

This article has been dead for over three months: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
View similar articles that have also been tagged:
 
© 2013 DaniWeb® LLC
Page rendered in 0.0870 seconds using 2.75MB