简体   繁体   English

任何类似于“何时”的陈述-javascript

[英]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.

相关问题 有没有类似于JavaScript中的ViewHolder的功能? - Is there any functionality similar to ViewHolder in JavaScript? 任何JavaScript语句都是表达式吗? - is any JavaScript statement an expression? Javascript文件有没有类似的JAVADOCS脚本/工具? - Is there any similar JAVADOCS script/tool for Javascript files? javascript中是否有类似于Java中的JFugue的库? - Is there any library in javascript that is similar to JFugue in Java? 关于Javascript中的Python的上下文管理器有什么提示吗? - Any tips on context manager similar to Python in Javascript? JavaScript是否具有与LINQ的Select语句类似的功能? - Does JavaScript have a similar capability as LINQ's Select statement? javascript:是否有用于构建类似于ebay的搜索过滤前端的UI库? - javascript: any UI libraries for building a search filtering frontend similar to ebay? JavaScript中是否有任何与JAVA Servlet中的application对象相似的对象 - Is there any object in `JavaScript` that is similar to `application` object in `JAVA Servlet` JavaScript中是否有类似utf8-encode(php)的功能? - Is there any similar function like utf8-encode(php) in javascript? 检查是否在任何if语句中使用了Javascript变量 - Check if a Javascript variable is used in any if statement
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM