0

I have function that is used from few different places in the system. All I need now is to return true or false depends if function completed successfully or not. here is example of what I have so far:

function userUnlock(userID){
    var fnResult;

    $.ajax({
        type: 'POST',
        url: 'Application.cfc?method=removeLock',
        data: {'userID':userID},
        dataType: 'json'
    }).done(function(obj){
        if(obj.STATUS == 200){
            fnResult = true;
        }else{
            fnResult = false;
        }
    }).fail(function(jqXHR, textStatus, errorThrown){
        alert("Error: "+errorThrown);
    });

    return fnResult;
}

Here is my onClick fucntion:

$("#unlockBtn").on("click", function(){
    var userID = $.trim($("#userID").val());

    if(userID){
        console.log(userUnlock(userID));
        $("#unlockBtn").prop("disabled",true);
        $('#searchMsg').addClass("success").show().text("User is unlocked.").delay(3000).fadeOut('slow').queue(function(){
            $(this).removeClass("success").dequeue();
            $("#unlockBtn").css("display","none");
        });
    }else{
        $('#searchMsg').addClass("error").show().text("Error.").delay(3000).fadeOut('slow').queue(function(){
            $(this).removeClass("error").dequeue();
        });
    }
});

In my onClick function console.log() returns undefined. I'm not sure why that's the case since I have return the variable that is Boolean. Also I was wondering if there is a way to prevent multiple requests onClick. For example if user by accident clicked few times. One way is to set button attribute disabled and I'm doing that but I want to make sure that my function prevents multiple requests. if anyone can provide some example that would be great. Thank you.

espresso_coffee
  • 5,980
  • 11
  • 83
  • 193

0 Answers0