0

是否可以对链接的点击进行去抖动?如果用户在 pjax 链接上点击太多次太快,它将破坏新内容的加载。

$(document).on('click', 'a[data-pjax]', loadNewContent);

var $target = $('main.content section.context'),
$fake = $('main.fake'),
$fakeContext = $('main.fake section.context');

function loadNewContent() {

    event.preventDefault();

    var $this = $(this),
    url = $this.attr('href');

    $fake.addClass('is--loading');

    $.pjax({
        url: url,
        fragment: 'body',
        container: $fakeContext
    });

    $fake.one(transitionEnd, function() {
        $target.html($fake.find('section.context').html());
        $fake.removeClass('is--loading');
        $fake.off(transitionEnd);
    });

}

有什么想法吗?我试过这个,但它阻止了 loadNewContent 的触发。( https://github.com/cowboy/jquery-throttle-debounce )

$(document).on('click', 'a[data-pjax]', $.debounce(1000, true, function() {
    loadNewContent();
}));
4

1 回答 1

2

像这样的东西会起作用:

var callWaiting = false;
callAjax() {
  if(!callWaiting) {
    callWaiting = true;
    makeHttpCall(url, data, function(response) {callWaiting = false;});
    callWaiting = false;
  }
}
于 2017-03-23T14:24:54.923 回答