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

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-12-2012, 11:43 AM   PM User | #1
cyphix
Regular Coder

 
Join Date: Apr 2004
Posts: 682
Thanks: 24
Thanked 1 Time in 1 Post
cyphix is an unknown quantity at this point
Getting portions of javascript to fire before entire function(s) finishes

I am changing a image via jQuery, but the image takes a few seconds to load and hence appears like the click did nothing, so I needed to input a temporary "loading" image to show the user that the image is loading.

However as I have experienced in the past JS doesn't really fire things as things happen, it waits until the full function is done before things happen; I recall coming up with a solution in the past but I just can't remember what it was.

I have this which does each task, but the user never sees the loading image because it is replaced by the new image before the user even sees anything.

Code:
function changeMainImage(image) {

    // Set temporary loading image
    jQuery('#main_image').attr('src', 'images/loading.jpeg');    

    // Set big image
    var big_image = image + '-big.png'

    // Update main image url
    jQuery('#main_image').attr('src', big_image);

}
I have tried putting the below into a separate function:

Code:
// Set temporary loading image
jQuery('#main_image').attr('src', 'images/loading.jpeg');
..and calling it before the other function like so..

Code:
onclick="setLoadingIMage(); changeMainImage('images/ai');"
but that didn't change anything.

Could anyone tell me what I need to do to achieve the effect I want?
cyphix is offline   Reply With Quote
Old 12-12-2012, 11:57 AM   PM User | #2
AndrewGSW
Senior Coder

 
Join Date: Apr 2011
Location: London, England
Posts: 2,120
Thanks: 15
Thanked 354 Times in 353 Posts
AndrewGSW will become famous soon enough
jQuery (and JS) do not wait for the function to complete before executing statements.

A couple of ways of doing this this are outlined here.

You might also consider pre-caching the big image(s). That is, creating new Image()'s and setting their src to the big images as, or just after, the page has loaded.
__________________
"I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.
Validate your HTML and CSS
AndrewGSW is offline   Reply With Quote
Users who have thanked AndrewGSW for this post:
cyphix (12-12-2012)
Old 12-12-2012, 12:35 PM   PM User | #3
cyphix
Regular Coder

 
Join Date: Apr 2004
Posts: 682
Thanks: 24
Thanked 1 Time in 1 Post
cyphix is an unknown quantity at this point
Perhaps I didn't use the correct wording, but as I understand it nothing is returned to "the screen" until it is done.

Have considered loading the images when the page does, but I chose to avoid this as the user may not even want to look at all the images (can be up to 12) and thus be an overhead we do not need.

Thanks for the link though - I'll check it out!
cyphix 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 04:51 AM.


Advertisement
Log in to turn off these ads.