Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I get this javascript to run every second? [duplicate]

How do I get this javascript to run every second?

source code:

<script type="text/javascript"> $(function() {     //More Button     $('.more').live("click",function()  {         var ID = $(this).attr("id");         if(ID) {             $("#more"+ID).html('<img src="moreajax.gif" />');              $.ajax({                 type: "POST",                 url: "ajax_more.php",                 data: "lastmsg="+ ID,                  cache: false,                 success: function(html){                     $("ol#updates").prepend(html);                     $("#more"+ID).remove();                 }             });         } else {             $(".morebox").html('no posts to display');         }          return false;      }); });  </script> 
like image 588
user663049 Avatar asked Apr 12 '11 16:04

user663049


People also ask

How do you make a function run every second in JavaScript?

Use setInterval() to run a piece of code every x milliseconds. You can wrap the code you want to run every second in a function called runFunction . setTimeout(runFunction,1000) please.

Which window method repeatedly calls a function f every 0.1 second?

setInterval() The setInterval() method, offered on the Window and Worker interfaces, repeatedly calls a function or executes a code snippet, with a fixed time delay between each call.

How do you call a function again and again?

Answer: Use the JavaScript setInterval() method You can use the JavaScript setInterval() method to execute a function repeatedly after a certain time period. The setInterval() method requires two parameters first one is typically a function or an expression and the other is time delay in milliseconds.

How define a function in JavaScript?

A JavaScript function is defined with the function keyword, followed by a name, followed by parentheses (). Function names can contain letters, digits, underscores, and dollar signs (same rules as variables). The parentheses may include parameter names separated by commas: (parameter1, parameter2, ...)


1 Answers

Use setInterval() to run a piece of code every x milliseconds.

You can wrap the code you want to run every second in a function called runFunction.

So it would be:

var t=setInterval(runFunction,1000); 

And to stop it, you can run:

clearInterval(t); 
like image 54
Mike Lewis Avatar answered Sep 19 '22 06:09

Mike Lewis