[英]Any statement similar to “when” - javascript
Do you know a way to be constantly checking something? 您知道一种不断检查某事的方法吗? I am using a plugin of ".scollto" and I want to perform a function when the plugin which has scrolled reaches its destiny.
我正在使用“ .scollto”插件,并且要在滚动的插件达到其预定目标时执行功能。 So I thought there may be a statement like "when" or something, like
所以我认为可能会有类似“何时”的声明或类似
"when" ($(window).scrolltop() == destiny) { //do something}
“何时”($(window).scrolltop()==命运){//做某事}
If I use IF/ELSE statements, it will check in the moment the code is read, but i want to check it once a while the plugin scrolls, or constantly. 如果我使用IF / ELSE语句,它将在读取代码的那一刻检查,但是我想在插件滚动或不停滚动时检查一下。 I don't care, but I want to get the action.
我不在乎,但我想采取行动。
maybe it could be 也许是
scroll();
setTimeout(check(), 2000);
What do you think? 你怎么看? does anyone know anything similar?
有谁知道类似的东西吗? Thank you.
谢谢。
No, there's nothing built-in that does this. 不,没有内置功能可以做到这一点。
Usually you write a handler for whatever event might change the state that you're interested in. Every time the event fires, your handler checks for the condition being met. 通常,您为任何可能更改您感兴趣的状态的事件编写处理程序。每次触发该事件时,您的处理程序都会检查是否满足条件。 For instance, using the scrollbar triggers the
scroll
event. 例如,使用滚动条触发
scroll
事件。
If there's no such event, you can use setInterval
to run a function periodically, and it can check for the condition you want. 如果没有此类事件,则可以使用
setInterval
定期运行一个函数,它可以检查所需的条件。
Do you want other things to be going on while the checking is going on? 您是否希望在检查过程中进行其他操作? If not, you could simply use a loop.
如果没有,您可以简单地使用循环。 Otherwise, look up what an instance is.
否则,请查找实例是什么。
Bind a function to the window.onscroll() handler. 将函数绑定到window.onscroll()处理程序。
make the first line of the function check to see where it's scrolled. 使函数的第一行检查以查看其滚动位置。
window.onscroll = function (e) {
if ($(window).scrolltop() == destiny) { //do something
}
}
everytime the window scrolls it will call that function. 每当窗口滚动时,它将调用该函数。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.