すべての検証が適切な場合にメールを送信するコードがあります。ユーザーが複数回クリックするのをブロックするだけです。したがって、デバウンスを使用してみましたが、デバウンスが関数を呼び出していることを確認しましたが、どちらも役に立ちません。私は何を間違っていますか?すばやく連続してクリックすると、ポップアップが常に表示されます。
var bookDiningBtnHandler = function() {
Ti.API.info('book dining clicked!');
var btnContext = this, message = {};
btnContext.touchEnabled = false;
hideKeyBoard();
// Avoid overlap of keyboard with the prompt..
removeDatePicker();
// Avoid overlap of picker with the prompt..
var isValidEmail = procs.checkValidEmail(email.value);
var isIntPositive = procs.isPositiveInteger(guest.value);
//var isValid = false;
if (name.value != '' && isValidEmail != false && isIntPositive != false && guest.value > 0 && date.text != 'Date' && time.text != 'Time') {
if (guest.value > guestLimit) {
message = {
"ref" : 0,
"title" : "Attention",
"text" : "Booking for more than 6, please check!"
};
customAlert(message);
btnContext.touchEnabled = true;
return;
}
booking = {
"action" : "diningBook",
"emailReservations" : Alloy.Globals.data.dining.emailReservation,
"name" : name.value,
"email" : email.value,
"guest" : guest.value,
"date" : date.text,
"time" : time.text
};
procs.sendEmail(booking, function(e) {
treatTheAnswer(e);
customAlert(message);
btnContext.touchEnabled = true;
});
function treatTheAnswer(resultSentEmail) {
if (resultSentEmail) {
message = {
"ref" : 0,
"title" : "Booking sent",
"text" : "A member of our team will contact you to confirm your booking"
};
} else {
message = {
"ref" : 1,
"title" : "Error ",
"text" : "Something went wrong. Please try again later!"
};
}
}
} else {
message = {
"ref" : 0,
"title" : "Missed info",
"text" : "Missing information, Please fill in all fields!"
};
if (!isValidEmail && isIntPositive) {
message = {
"ref" : 1,
"title" : "Error",
"text" : " Invalid Email Address. Please re-enter valid Address!"
};
}
customAlert(message);
btnContext.touchEnabled = true;
}
};
function debounce(func, wait, immediate){
// Returns a function, that, as long as it continues to be invoked, will not
// be triggered. The function will be called after it stops being called for
// N milliseconds. If `immediate` is passed, trigger the function on the
// leading edge, instead of the trailing.
var timeout = null;
return function() {
var context = this, args = arguments;
clearTimeout(timeout);
timeout = setTimeout(function() {
timeout = null;
if (!immediate)
func.apply(context, args);
}, wait);
if (immediate && !timeout)
func.apply(context, args);
};
};
v6.addEventListener('click', procs.debounce(bookDiningBtnHandler, 1000));