-
Notifications
You must be signed in to change notification settings - Fork 0
/
jquery.preventDoubleSubmission.js
81 lines (67 loc) · 1.76 KB
/
jquery.preventDoubleSubmission.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
/*!
* jQuery plugin to prevent double submission of forms 1.0.0
*
* https://github.com/ychumak/preventDoubleSubmission
*
* Copyright 2014 Yuriy Chumak
* Released under the MIT license:
* http://www.opensource.org/licenses/mit-license.php
*/
;(function( $ ) {
$.fn.preventDoubleSubmission = function(config) {
var defaults = {
doThrottle: false,
throttleTime: 2000 // 2 seconds by default
};
var last_clicked = undefined;
var time_since_clicked = undefined;
var $form = undefined;
if ( config ) $.extend(defaults, config);
enableForm = function() {
if ($form) {
$form.data('submitted', false);
}
};
disableForm = function() {
if ($form) {
if ($form) $form.data('submitted', true);
}
};
enableThrottle = function(timeOut) {
defaults.doThrottle = true;
if (timeOut) {
defaults.throttleTime = timeOut;
}
};
disableThrottle = function() {
defaults.doThrottle = false;
defaults.throttleTime = 2000;
};
$(this).on('submit', function(e){
$form = $(this);
if (defaults.doThrottle) {
if (last_clicked) {
time_since_clicked = e.timeStamp - last_clicked;
}
last_clicked = e.timeStamp;
if(time_since_clicked < defaults.throttleTime) {
// submitted so fast - don't submit again
alert('catch !!!');
e.preventDefault();
return false;
}
} else {
if ($form.data('submitted') === true) {
// Previously submitted - don't submit again
e.preventDefault();
} else {
// Mark it so that the next submit can be ignored
//$form.data('submitted', true);
disableForm();
}
}
});
// Keep chainability
return this;
};
})( jQuery );