0

I have a code which sends a mail if all the validations are proper. I simply need to block the user from clicking it multiple times. Hence i tried using debounce and confirmed that debounce is calling my function but it doesnt help either. What wrong am i doing? I always have the popup appearing one over other when quick successive clicks are made.

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));
ABL
  • 3
  • 2

0 Answers0