Javascript 对Ajax的多次调用,而不是一次调用

Javascript 对Ajax的多次调用,而不是一次调用,javascript,jquery,ajax,Javascript,Jquery,Ajax,我有下面的代码,它在大多数情况下都可以正常工作,但我遇到的问题是鼠标悬停。悬停10秒后,内容将展开,然后调用ajax。Ajax调用了5次而不是一次 我不知道为什么它一直打5次电话。有人能帮我解决这个问题,让ajax调用只运行一次吗 下面是我的代码片段,完整的工作在这里 每次鼠标在元素上移动时都会发生鼠标悬停事件。您需要的是一个boolean,它检查您是否发送了AJAX请求,如果它没有发送AJAX请求,则忽略该事件 $(".previewCard-content").hide(); var tim

我有下面的代码,它在大多数情况下都可以正常工作,但我遇到的问题是鼠标悬停。悬停10秒后,内容将展开,然后调用ajax。Ajax调用了5次而不是一次

我不知道为什么它一直打5次电话。有人能帮我解决这个问题,让ajax调用只运行一次吗

下面是我的代码片段,完整的工作在这里


每次鼠标在元素上移动时都会发生鼠标悬停事件。您需要的是一个
boolean
,它检查您是否发送了
AJAX
请求,如果它没有发送
AJAX
请求,则忽略该事件

$(".previewCard-content").hide();
var timeo = null;
var ajaxSent = false
$("body").on("mouseenter", ".previewCard-showhide", function() { // Use rather mouseenter!
    var $that = $(this); // Store the `this` reference
    clearTimeout(timeo); // Clear existent timeout on m.Enter 

    timeo = setTimeout(function() { // Before setting a new one
        $that.hide().closest('p').next(".previewCard-content").slideDown("slow");

        /**************** AJAX CALL********************/
        var LinkTextVal = $that.closest('.previewCard-b').find('.previewCardPageLink').text();
        console.log(" LinkTextVal  " + LinkTextVal);
        var descPageName = LinkTextVal + ' | About';
        if ($('#userID').val() !== '' && $('#userID').val() !== undefined && $('#userID').val() !== null && !ajaxSent) {
            ajaxSent = true;
            $.ajax({
                url: '/localhost/biz/actions/searchBookmark' + '?cachestop=' + nocache,
                type: "get",
                data: {
                    bookmarkName: descPageName
                },
                success: function(response) {
                    if (response === true) {
                        $that.parents('.previewCard-b').find('.save a').addClass('saved');
                        $that.parents('.previewCard-b').find('.save a').addClass('active');
                        $that.parents('.previewCard-b').find('.save a').find(".action-text").text("Saved");
                    }

                },
                error: function(e) {
                    console.log('Unable to check if a bookmark exists for the user.');
                }
            });
        }
        /***************** END AJaX/SAVE BUTTON ************/

    }, 1000);
}).on("mouseleave", ".previewCard-showhide", function() { // mouse leaves? Clear the timeout again!
    clearTimeout(timeo);
});
$(".close-btn").on("click", function() {
    var $itemB = $(that).closest(".previewCard-b");
    $itemB.find(".previewCard-content").slideUp();
    $itemB.find(".previewCard-showhide").show();
});
$(".previewCard-content").hide();
var timeo = null;
var ajaxSent = false
$("body").on("mouseenter", ".previewCard-showhide", function() { // Use rather mouseenter!
    var $that = $(this); // Store the `this` reference
    clearTimeout(timeo); // Clear existent timeout on m.Enter 

    timeo = setTimeout(function() { // Before setting a new one
        $that.hide().closest('p').next(".previewCard-content").slideDown("slow");

        /**************** AJAX CALL********************/
        var LinkTextVal = $that.closest('.previewCard-b').find('.previewCardPageLink').text();
        console.log(" LinkTextVal  " + LinkTextVal);
        var descPageName = LinkTextVal + ' | About';
        if ($('#userID').val() !== '' && $('#userID').val() !== undefined && $('#userID').val() !== null && !ajaxSent) {
            ajaxSent = true;
            $.ajax({
                url: '/localhost/biz/actions/searchBookmark' + '?cachestop=' + nocache,
                type: "get",
                data: {
                    bookmarkName: descPageName
                },
                success: function(response) {
                    if (response === true) {
                        $that.parents('.previewCard-b').find('.save a').addClass('saved');
                        $that.parents('.previewCard-b').find('.save a').addClass('active');
                        $that.parents('.previewCard-b').find('.save a').find(".action-text").text("Saved");
                    }

                },
                error: function(e) {
                    console.log('Unable to check if a bookmark exists for the user.');
                }
            });
        }
        /***************** END AJaX/SAVE BUTTON ************/

    }, 1000);
}).on("mouseleave", ".previewCard-showhide", function() { // mouse leaves? Clear the timeout again!
    clearTimeout(timeo);
});
$(".close-btn").on("click", function() {
    var $itemB = $(that).closest(".previewCard-b");
    $itemB.find(".previewCard-content").slideUp();
    $itemB.find(".previewCard-showhide").show();
});