暂停鼠标/单击直到$ .get完成

时间:2013-07-26 09:32:38

标签: javascript jquery mouseevent

在我的客户端页面上,我想在每次用户点击某些内容时执行我的功能。函数将此元素标记名称发送到数据库。为避免覆盖客户端页面单击事件处理程序,我使用mousedown。我的想法:

jQuery('body').on('mousedown', function(event){
    var that = event.target,
        thatTag = event.target.nodeName;

    jQuery.get( 
        "http://example.com",
        { parameter: thatTag },
        function() {
            //It hasn't time to execute, because mouseup is auto executed and click action change location (if I click anchor)
        }
    );
});

那么,在我的GET操作完成之前,暂停/阻止mouseup / click是否有任何选项?

1 个答案:

答案 0 :(得分:0)

首次执行时,您需要使用.off取消绑定mousedown处理程序,然后在.get函数完成时再次重新绑定:

$(function() {
    // On page load, bind the first instance of the event
    $("body").on("mousedown", handleMouseDown);
});

function handleMouseDown(event) {
    // Detach the event so that is isn't handled again until the .get finishes
    $("body").off("mousedown", handleMouseDown);
    var that = event.target,
        thatTag = event.target.nodeName;

    jQuery.get( 
        "http://example.com",
        { parameter: thatTag },
        function() {
            // Re-attach the event now that the .get has finished
            $("body").on("mousedown", handleMouseDown);
        }
    );
}