[英]Check browser width and fire event only once
How do I only fire an event once, once the user hits below 669px?一旦用户点击低于 669 像素,我如何只触发一次事件?
Here's the code I'm working with:这是我正在使用的代码:
const checkWindowWdith = () => {
console.log(document.documentElement.clientWidth);
if (document.documentElement.clientWidth < 669) {
//DO SOMETHING HERE
};
};
//HERE I AM WAIT FOR THE USER TO FINISH RESIZING
var doit;
window.onresize = function(){
clearTimeout(doit);
doit = setTimeout(checkWindowWdith, 100);
};
I think you need to create a var who is set to false if the event was start and set to true if he never been start like this:我认为你需要创建一个变量,如果事件开始,则设置为 false,如果他从未像这样开始,则设置为 true:
var firstStartEvent = true const checkWindowWdith = () => { console.log(document.documentElement.clientWidth); if (document.documentElement.clientWidth < 669) { firstStartEvent = false; //DO SOMETHING HERE }; }; //HERE I AM WAIT FOR THE USER TO FINISH RESIZING var doit; window.onresize = function(){ clearTimeout(doit); if (firstStartEvent) { doit = setTimeout(checkWindowWdith, 100); } };
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.