Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8949813
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T13:18:21+00:00 2026-06-15T13:18:21+00:00

I have a poll() function: $(document).ready(function(){ poll(10); $(#refresh).click(function(){ poll(10); }); }); function poll(timeout){ return

  • 0

I have a poll() function:

$("document").ready(function(){
   poll(10);
   $("#refresh").click(function(){ poll(10); });
});

function poll(timeout){
   return setTimeout(function(){
      if (timeout == 10){
         timeout = 10000;
      }
      $.ajax({ url: "/ajax/livedata.php", success: function(data){
         $("#result").html(data);
         poll(timeout);
      }, dataType: "json"});
   }, timeout);
}

So, what happens there:

1) on page load, poll() is called and the loop starts being executed.

2) on #refresh click, poll() is called right at that moment to request new data immediately.

The problem: whenever I click #refresh to request new data, the poll() is called again, but fired multiple times (first time the initial + every new click). Thus it fires not every 10 seconds as expected, but multiple times every 10 seconds (depending on how many times I clicked on #refresh).

How can I fix this, so that whenever I click #refresh, only 1 instance will be left looping?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T13:18:22+00:00Added an answer on June 15, 2026 at 1:18 pm

    first store your setTimeout Object to a variable somewhat like this

    var a = poll(10);
    

    then stop it upon clicking refresh by using this code

    clearTimeout(a);
    

    So it will be like this

    $("document").ready(function(){
       var a = null;   
       $("#refresh").click(function(){ 
          if(a != null){
               clearTimeout(a);
               a = null;
          }
          a = poll(10); 
       });
    });
    

    reference:

    http://www.w3schools.com/jsref/met_win_cleartimeout.asp

    http://www.w3schools.com/js/tryit.asp?filename=tryjs_timing_stop

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following poll() function: var pollTimeout = 5000; (function poll(){ setTimeout(function(){ $.ajax({
I'm trying to create a simple poll function using php and sql. I have
I have a php/mysql poll that I'm trying to modify. It sets a unique
I have index.php page which makes a ajax call to thread.php by sending the
I have the following code: XService.start = function() { setInterval(XService.poll(), XService.pollInterval); } This works
In the pseudocode below I have a poll() function that gets called forever in
I have function which is call recursively by ajax complete callback, and I need
my updated full script: $(document).ready(function() { $.getJSON('table.json',function(data){ $('#mytable').empty(); var html = ''; html +=
I have just been reading the section on the poll() function in Advanced Unix
Say I have a database called Poll and have these two table structures. poll

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.