Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
53
rated 0 times [  57] [ 4]  / answers: 1 / hits: 163389  / 13 Years ago, tue, april 12, 2011, 12:00:00

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>

More From » ajax

 Answers
9

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);

[#92779] Monday, April 11, 2011, 13 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
catrinas

Total Points: 587
Total Questions: 100
Total Answers: 105

Location: Rwanda
Member since Thu, Feb 10, 2022
2 Years ago
;