Skip to content

Instantly share code, notes, and snippets.

@zzuhan
Last active December 30, 2015 08:29
Show Gist options
  • Save zzuhan/7802671 to your computer and use it in GitHub Desktop.
Save zzuhan/7802671 to your computer and use it in GitHub Desktop.
防止按钮被多次点击功能的封装
function isInProgress(){
return $submit.prop('disabled') === true;
}
function markInProgress(){
$submit.prop('disabled', true);
}
function markProgressDone(){
$submit.prop('disabled', false);
}
/**
* 是否会引起误导,因为本来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