Last active
December 22, 2015 03:09
-
-
Save magicapple/6408675 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // method of getCookie | |
| function getCookie( name ) { | |
| var parts = document.cookie.split(name + "="); | |
| if (parts.length == 2) return parts.pop().split(";").shift(); | |
| } | |
| function expireCookie( cName ) { | |
| document.cookie = | |
| encodeURIComponent( cName ) + | |
| "=deleted; expires=" + | |
| new Date( 0 ).toUTCString(); | |
| } | |
| function setCursor( docStyle, buttonStyle ) { | |
| document.getElementById( "doc" ).style.cursor = docStyle; | |
| document.getElementById( "button-id" ).style.cursor = buttonStyle; | |
| } | |
| function setFormToken() { | |
| var downloadToken = new Date().getTime(); | |
| document.getElementById( "downloadToken" ).value = downloadToken; | |
| return downloadToken; | |
| } | |
| var downloadTimer; | |
| var attempts = 30; | |
| // Prevents double-submits by waiting for a cookie from the server. | |
| function blockResubmit() { | |
| var downloadToken = setFormToken(); | |
| setCursor( "wait", "wait" ); | |
| downloadTimer = window.setInterval( function() { | |
| var token = getCookie( "downloadToken" ); | |
| if( (token == downloadToken) || (attempts == 0) ) { | |
| unblockSubmit(); | |
| } | |
| attempts--; | |
| }, 1000 ); | |
| } | |
| function unblockSubmit() { | |
| setCursor( "auto", "pointer" ); | |
| window.clearInterval( downloadTimer ); | |
| expireCookie( "downloadToken" ); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment