如何在滚动停止时触发ajax请求? [英] How to trigger ajax request on scroll stop?

查看:23
本文介绍了如何在滚动停止时触发ajax请求?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在窗口滚动时,我正在执行这样的 ajax 请求

On window scroll I'm doing ajax request like this

$(window).scroll(function(){
 //doing ajax request
});

但它正在为滚动事件创建多个ajax请求.有没有像 onscrollstop 这样的事件或者只有在窗口滚动结束后才会触发的事件.或者有没有其他方法可以处理相同的情况.请提出建议.

but it is creating multiple ajax request for scroll event. Is there any event like onscrollstop or something which only trigger after window scroll end. Or is there any other way I can handle the same scenario. Please suggest.

推荐答案

我不相信有 onscrollstop 侦听器,但是您可以通过设置超时并在滚动继续时清除它来模拟它.

I don't believe there's an onscrollstop listener, but you can emulate it by setting a timeout and clearing it if scrolling continues.

var timeout = null;

$(window).scroll(function() {
    clearTimeout(timeout);
    timeout = setTimeout(function() {
        // do your ajax request
    }, 100);
});

这篇关于如何在滚动停止时触发ajax请求?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆