Skip to content

Instantly share code, notes, and snippets.

@toamitkumar
Created March 12, 2012 00:04
Show Gist options
  • Save toamitkumar/2018773 to your computer and use it in GitHub Desktop.
Save toamitkumar/2018773 to your computer and use it in GitHub Desktop.
/* https://github.com/carhartl/jquery-cookie */
$("a.linkDownload").click(function(event) {
.... other action on click of link
$(this).bindDownloadEvents(event); // bind the form submit event
});
$.fn.bindDownloadEvents = function(event) {
event.preventDefault();
showDownloadIndicator(this);
.... //Add the necessary form elements including json
$("submit the form");
};
function showDownloadIndicator(link) {
var timeCounter = 0; // used for keeping track of time elapsed, notify the user in case of delays
disableLinkAndShowIndicator(link); // disable the download link and start the "waiting" indicator
// Start the setInterval to check for a cookie every 1 sec
fileDownloadCheckTimer = window.setInterval(function () {
timeCounter++;
var cookieValue = $.cookie('fileDownloadStatus');
if(timeCounter >= 20) { // timed out
alert("Error", "We are experiencing issues with file download, please try again later.");
finishDownloadAndRemoveIndicator(link);
}
else if (cookieValue == "complete") {
// server response has the cookie, do the cleanup and remove the cookie
finishDownloadAndRemoveIndicator(link);
}
}, 1000);
}
function finishDownloadAndRemoveIndicator(link) {
$.cookie('fileDownloadStatus', null); // remove the cookie
window.clearInterval(fileDownloadCheckTimer); // clear the setInterval
removeIndicator(link); // remove the "waiting indicator"
$(link).unbind("click").bind("click", function(e) { // bind the click event back to the link
$(this).bindDownloadEvents(e);
});
}
function disableLinkAndShowIndicator(link){
showIndicator(link);
$(link).unbind("click").bind("click", function(e) { e.preventDefault();});
}
function showIndicator(link) {
... code to show the indicator
}
function removeIndicator(link) {
... code to remove/hide the indicator
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment