javascript 刷新 jQuery 函数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4903847/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 15:17:46  来源:igfitidea点击:

Refresh a jQuery function

javascriptjqueryjquery-pluginsrefresh

提问by Andrea Turri

Is possible refresh a function every x seconds or refresh it on single event?

是否可以每 x 秒刷新一次函数或在单​​个事件上刷新它?

I will explain:

我会解释:

I have a function that makes pagination on my website, inside every div that are "pages" I have some pictures where I have added LightBox.

我有一个功能可以在我的网站上进行分页,在作为“页面”的每个 div 中,我有一些添加了 LightBox 的图片。

Everything works nice on the first div (page) but when I change the page it doesnt' work anymore.

在第一个 div(页面)上一切正常,但是当我更改页面时,它不再起作用。

I tried to fix in this way:

我试图以这种方式修复:

$('.pagination a').click(function () {
    initShadow();
});

In this way it work on the pageLoad and on the first page that I change, than it stop again.

通过这种方式,它可以在 pageLoad 和我更改的第一页上工作,然后再次停止。

So I need to fix this issue, everytime I change the "page" I would like it works fine.

所以我需要解决这个问题,每次我更改“页面”时,我都希望它工作正常。

Is possible to refresh a function every x second or to refresh it everytime I click on the pagination buttons?

是否可以每 x 秒刷新一次函数或每次单击分页按钮时刷新它?

回答by jondavidjohn

I believe setInterval()is what you are looking for. If you want to take the timed route.

我相信setInterval()这就是你正在寻找的。如果你想走定时路线。

setInterval(functionToExecute, 1000); 
//this would call 'functionToExecute' every 1000 milliseconds

Otherwise, I would recommend using .live()instead of .click()just incase your pagination links are being removed and recreated by the plugin. Thought this might be the case when you said it worked 1 additional time after you added this click event.

否则,我建议您使用.live()而不是.click()仅在插件删除和重新创建分页链接的情况下使用。当您说在添加此点击事件后它又工作了 1 次时,您认为可能是这种情况。

example

例子

$('.pagination a').live('click',function () {
    initShadow();
});

回答by Gibin Ealias

You can use the .on()method of jQuery.

你可以使用.on()jQuery的方法。

$(document).on('click', '.pagination a', function () {
    initShadow();
});

Hope this helps.

希望这可以帮助。