简体   繁体   English

当用户向下滚动x秒时触发事件

[英]Trigger an event when user scrolls down for x seconds

I need to trigger an event, once, after the user scrolls down for 1 second. 用户向下滚动1秒钟后,我需要触发一次事件。 I used the following code, but it triggers the event numerous times, otherwise I think that it sorts of corresponds to what I need? 我使用了以下代码,但是它多次触发该事件,否则我认为它与我需要的内容相对应吗?

Thanks ! 谢谢 !

var firstScroll = false;
window.onscroll = function() {myFunction()};

function myFunction() {
    if (!firstScroll){
        setTimeout(function() {
            alert("x");
            firstScroll = true;  
        }, 1000);
    }
}

Try moving your scroll check outside of the setTimeout. 尝试将滚动检查移到setTimeout之外。

 var firstScroll = false; window.onscroll = function() {myFunction()}; function myFunction() { if (!firstScroll){ firstScroll = true; setTimeout(function() { alert("x"); }, 1000); } } 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM