Last active
December 30, 2015 08:29
-
-
Save zzuhan/7802671 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
function isInProgress(){ | |
return $submit.prop('disabled') === true; | |
} | |
function markInProgress(){ | |
$submit.prop('disabled', true); | |
} | |
function markProgressDone(){ | |
$submit.prop('disabled', false); | |
} |
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
/** | |
* 是否会引起误导,因为本来js是单线程的,你来了一个progress.start(),progress一直是启动的。 | |
* isActionInProgress; | |
* progress.handle(); | |
* progress.done(); | |
* / | |
var progress = (function(){ | |
var running = false; | |
function isInProgress(){ | |
return running === true; | |
} | |
function markInProgress(){ | |
running = true; | |
} | |
function markProgressDone(){ | |
running = false; | |
} | |
return { | |
start: markInProgress, | |
// or destroy | |
stop: markProgressDone, | |
isRunning: isInProgress | |
} | |
})(); | |
$submit = $('#submit'); | |
$submit.click(function(e){ | |
if(progress.isRunning()) return; | |
progress.start(); | |
// simulate ajax spend 2000ms | |
setTimeout(function(){ | |
progress.stop(); | |
}, 2000); | |
e.preventDefault(); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment