Avoid form multiple submissions and re-enable when done $(document).ready(function(){ //preload wait img var $waitImg = $('').attr('src', '/img/wait.gif') $('.wait').click(function(){ $(':submit').hide(); $(this).after($('Processing, please wait...').prepend($waitImg)); }); }); function downloadStarted() { var $def = $.Deferred(); var check = function() { $.get('/ajax/check-download-status', function(response) { if (response.started) { $def.resolve(); } else { //Check again in 3 seconds setTimeout(check, 3000); } }, 'json'); }; check(); return $def.promise(); } var success = function() { /* reset/hide loading image */ } $.when(downloadStarted()).then(success); session_start(); $json['started'] = 0; if ($_SESSION['download_started']) $json['started'] = 1; return json_encode($json);
...
$(document).ready(function(){ var cookieCheckTimer; var cookieName = 'download_token'; var $wait = $('.wait'); var $waitMsg = $('Processing, please wait...').prepend( $('').attr('src', '/img/wait.gif').css({ 'vertical-align': 'middle', 'margin-right': '1em' })); //add hidden field to forms with .wait submit $wait.parent().each(function(){ $(this).append($('').attr({ 'type': 'hidden', 'name': cookieName, 'id': cookieName })); }); $wait.click(function(){ var token = new Date().getTime(); //set token value $('#' + cookieName).val(token); //hide submit buttons $(':submit').hide(); //append wait msg $(this).after($waitMsg); cookieCheckTimer = window.setInterval(function () { if ($.cookie(cookieName) == token){ //clear timer window.clearInterval(cookieCheckTimer); //clear cookie value $.cookie(cookieName, null); //detach wait msg $waitMsg.detach(); //show again submit buttons $(':submit').show(); } }, 1000); }); }); cookieName = 'download_token' #set file download coockie if asked if cookieName in request.params: response.set_cookie(cookieName, #str: see http://groups.google.com/group/pylons-discuss/browse_thread/thread/7d42f3b28bc6f447 str(request.params.get(self._downloadTokenName)), expires=datetime.now() + timedelta(minutes=15))