多次点击问题辩论不会奏效

时间:2014-09-25 11:11:12

标签: javascript jquery javascript-events debouncing

如果所有验证都合适,我有一个代码可以发送邮件。我只需要阻止用户多次点击它。因此我尝试使用去抖并确认去抖是调用我的功能,但它也没有帮助。我做错了什么?当快速连续点击时,我总是会弹出一个弹出窗口。

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));

0 个答案:

没有答案