Reply
JQuery - Load on one click?
Old 11-22-2008, 08:11 AM JQuery - Load on one click?
Galaxian's Avatar
Dingleberry!

Posts: 695
Name: Rich
Location: United Kingdom
This is suseptible to click spam, and loads multiple times when clicked. How'd I make it so it loads only once when it's loading in progress and is clicked multiple times?

Code:
loadingimg = '<img src="images/loading.gif" alt="Loading.." style="margin:auto;display:block;padding:15px;" />';
$(document).ready(function(){
    $('#rstory').html(loadingimg).load('ajax/randomstory.php', function() {
        $('#rstory').fadeIn('fast')
    });
    $('#getrstory').click(function() {
    $('#rstory').fadeOut('fast', function() {
        $('#rstoryload').html(loadingimg)
        $('#rstory').load('ajax/randomstory.php', function() {
        $('#rstoryload').html('');
        $('#rstory').fadeIn('fast');
        });
        });
    });
    
});
__________________
Galaxian is offline
Reply With Quote
View Public Profile Visit Galaxian's homepage!
 
When You Register, These Ads Go Away!
Old 11-22-2008, 09:41 AM Re: JQuery - Load on one click?
wayfarer07's Avatar
Power Hungry Seamonkey

Posts: 2,630
Name: Abel Mohler
Location: Asheville, North Carolina USA
If I understand you correctly:

Make a variable called "loading" that is set to false, but is made true on click, but made false again in the callback. Then, in the click event, only make the AJAX call and fadeOut() if !loading:

Code:
loadingimg = '<img src="images/loading.gif" alt="Loading.." style="margin:auto;display:block;padding:15px;" />';
$(document).ready(function(){
var loading=false;
    $('#rstory').html(loadingimg).load('ajax/randomstory.php', function() {
        $('#rstory').fadeIn('fast')
    });
    $('#getrstory').click(function() {
    if(!loading) {
    loading=true;
    $('#rstory').fadeOut('fast', function() {
        $('#rstoryload').html(loadingimg)
        $('#rstory').load('ajax/randomstory.php', function() {
        loading=false;
        $('#rstoryload').html('');
        $('#rstory').fadeIn('fast');
        });
        });
    }
    });
});
wayfarer07 is offline
Reply With Quote
View Public Profile Visit wayfarer07's homepage!
 
Old 11-22-2008, 12:02 PM Re: JQuery - Load on one click?
Galaxian's Avatar
Dingleberry!

Posts: 695
Name: Rich
Location: United Kingdom
Thanks, finished working solution is:

Code:
loadingimg = '<img src="images/loading.gif" alt="Loading.." style="margin:auto;display:block;padding:15px;" />';
$(document).ready(function(){
var loading=false;
    $('#rstory').html(loadingimg).load('ajax/randomstory.php', function() {
        $('#rstory').fadeIn('fast')
    });
    $('#getrstory').click(function() {
    if(!loading) {
    loading=true;
    $('#rstory').fadeOut('fast', function() {
        $('#rstoryload').html(loadingimg)
        $('#rstory').load('ajax/randomstory.php', function() {
        $('#rstoryload').html('');
        $('#rstory').fadeIn('fast');
        loading=false;
        });
        });
    }
    });    
});
loading=false needs to go further down than your posted one

Thanks!
__________________
Galaxian is offline
Reply With Quote
View Public Profile Visit Galaxian's homepage!
 
Old 11-22-2008, 01:32 PM Re: JQuery - Load on one click?
wayfarer07's Avatar
Power Hungry Seamonkey

Posts: 2,630
Name: Abel Mohler
Location: Asheville, North Carolina USA
Right on. I'm glad you figured it out. I use this type of technique a lot when I'm doing loading sequences that use events to trigger them.
wayfarer07 is offline
Reply With Quote
View Public Profile Visit wayfarer07's homepage!
 
Reply     « Reply to JQuery - Load on one click?
 

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off




   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML

 



Page generated in 0.10223 seconds with 12 queries