admin管理员组

文章数量:1289623

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

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.

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

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.

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?

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

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.

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

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.

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?

Share Improve this question edited Aug 29, 2011 at 21:05 jondavidjohn 62.4k21 gold badges120 silver badges159 bronze badges asked Feb 4, 2011 at 23:13 Andrea TurriAndrea Turri 6,5007 gold badges39 silver badges64 bronze badges 6
  • Is there one pagination button, or you have one on every page? – aorcsik Commented Feb 4, 2011 at 23:25
  • are these 'pages' being loaded in via ajax? or being built dynamically after the page has already loaded? – jondavidjohn Commented Feb 4, 2011 at 23:25
  • is a pagination, alot of pages... in everypage I want shadowbox (or other plugIns) to work... – Andrea Turri Commented Feb 4, 2011 at 23:26
  • @jondavidjohn is not loaded via Ajax, just simple jQuery/css – Andrea Turri Commented Feb 4, 2011 at 23:26
  • can we see the pagination code? – jondavidjohn Commented Feb 4, 2011 at 23:28
 |  Show 1 more ment

2 Answers 2

Reset to default 6

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

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

Otherwise, I would remend 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.

example

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

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

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

Hope this helps.

本文标签: javascriptRefresh a jQuery functionStack Overflow