Soliloquy - The Best Responsive WordPress Slider Plugin

Handy JavaScript Polling Function for Resize and Other Events

Share This Post

I use JavaScript quite often, and a frequent need is for me to poll when a screen is resized and fire some code (or event) at particular intervals.

This is often called “polling”, and I’ve created a handy JS function that you can use for all of your polling needs. Here is the polling variable function:

This variable takes a function and applies another function and polling timer based on parameters set. I use this most commonly with the window.resize event, so here is an example of how you would use this (I’m using jQuery here):

Here we poll the window.resize event every 25ms and fire our anonymous function that is passed into the poll variable. We can check for flags and other things before doing anything if necessary.

This function always comes in handy, so I hope you can make use of it too!

Have you enjoyed reading Handy JavaScript Polling Function for Resize and Other Events? Sign up below to get regular content updates and also get my "7 Tips to a Professional WordPress Setup" absolutely free!

About Thomas Griffin

I live and breathe WordPress. I create products around WordPress (Soliloquy and OptinMonster), contribute to WordPress core and do lots of fun development around WordPress in general. You can find me on Twitter, Facebook and Google+.

  • http://vugujranwala.com/Download/pastPapers/index.php?v=1&csid=CS201 wasifaliwasi

    A very awesome ! blog post. We are really grateful for your blog post. You will find a lot of approaches after visiting your post. I was exactly searching for. Thanks for such post and please keep it up.