Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Repeat code every 4 seconds

I want repeat this code every 4 seconds, how i can do it with javascript or jquery easly ? Thanks. :)

$.get("request2.php", function(vystup){
   if (vystup !== ""){
      $("#prompt").html(vystup);
      $("#prompt").animate({"top": "+=25px"}, 500).delay(2000).animate({"top": "-=25px"}, 500).delay(500).html("");
    }
});
like image 885
Gabriel Uhlíř Avatar asked Jun 19 '11 14:06

Gabriel Uhlíř


People also ask

How do I run code every second?

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.

How can we call a function which logs a message after every 5 seconds?

You can use setInterval() , the arguments are the same.

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 do you repeat a function in JavaScript?

repeat() is an inbuilt function in JavaScript which is used to build a new string containing a specified number of copies of the string on which this function has been called. Syntax: string. repeat(count);


1 Answers

Use setInterval function

setInterval( fn , miliseconds )

From MDC docs:

Summary

Calls a function repeatedly, with a fixed time delay between each call to that function.

Syntax

var intervalID = window.setInterval(func, delay[, param1, param2, ...]);
var intervalID = window.setInterval(code, delay);

where

intervalID is a unique interval ID you can pass to clearInterval().

func is the function you want to be called repeatedly.

code in the alternate syntax, is a string of code you want to be executed repeatedly. (Using this syntax is not recommended for the same reasons as using eval())

delay is the number of milliseconds (thousandths of a second) that the setInterval() function should wait before each call to func. As with setTimeout, there is a minimum delay enforced.

Note that passing additional parameters to the function in the first syntax does not work in Internet Explorer.

Example

// alerts "Hey" every second
setInterval(function() { alert("Hey"); }, 1000);
like image 183
BrunoLM Avatar answered Oct 06 '22 19:10

BrunoLM