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. So I thought there may be a statement like "when" or something, like
"when" ($(window).scrolltop() == destiny) { //do something}
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. 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.
33 Answers
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.
If there's no such event, you can use setInterval to run a function periodically, and it can check for the condition you want.
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.
0Bind a function to the window.onscroll() handler.
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.
2