21

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("");
    }
});
Gabriel Uhlíř
  • 609
  • 3
  • 9
  • 18
  • possible duplicate of [Jquery repeat function every 60 seconds](http://stackoverflow.com/questions/3138756/jquery-repeat-function-every-60-seconds) – mplungjan Jun 19 '11 at 14:52
  • And next time look for some identical questions too - just have a look to the right of this question – mplungjan Jun 19 '11 at 14:52
  • 2
    @Niklas, @BeingSimpler On the one hand, it's not great that the user has accepted no answers. However, the answers to the questions he's posted haven't been great, and no-one's ever asked him to accept answers before. He may not even know that it's possible. Your comments are a bit much. – lonesomeday Jun 19 '11 at 14:55

6 Answers6

36

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);
BrunoLM
  • 97,872
  • 84
  • 296
  • 452
27
setInterval(function(){
  // your code...
}, 4000);
gblazex
  • 49,155
  • 12
  • 98
  • 91
20

It's not too hard in javascript.

// declare your variable for the setInterval so that you can clear it later
var myInterval; 

// set your interval
myInterval = setInterval(whichFunction,4000);

whichFunction{
    // function code goes here
}

// this code clears your interval (myInterval)
window.clearInterval(myInterval); 

Hope this helps!

Jacob
  • 3,835
  • 8
  • 25
  • 25
12

Another possibility is to use setTimeout, but place it along with your code in a function that gets called recursively in the callback to the $.get() request.

This will ensure that the requests are a minimum of 4 seconds apart since the next request will not begin until the previous response was received.

 // v--------place your code in a function
function get_request() {
    $.get("request2.php", function(vystup){
       if (vystup !== ""){
          $("#prompt").html(vystup)
                      .animate({"top": "+=25px"}, 500)
                      .delay(2000)
                      .animate({"top": "-=25px"}, 500)
                      .delay(500)
                      .html("");
        }
        setTimeout( get_request, 4000 ); // <-- when you ge a response, call it
                                         //        again after a 4 second delay
    });
}

get_request();  // <-- start it off
user113716
  • 318,772
  • 63
  • 451
  • 440
2
const milliseconds = 4000 

setInterval(
  () => { 
  // self executing repeated code below

}, milliseconds);
jasonleonhard
  • 12,047
  • 89
  • 66
0

Call a Javascript function every 2 second continuously for 20 second.

var intervalPromise;
$scope.startTimer = function(fn, delay, timeoutTime) {
    intervalPromise = $interval(function() {
        fn();
        var currentTime = new Date().getTime() - $scope.startTime;
        if (currentTime > timeoutTime){
            $interval.cancel(intervalPromise);
          }                  
    }, delay);
};

$scope.startTimer(hello, 2000, 10000);

hello(){
  console.log("hello");
}
Ankit Tiwari
  • 41
  • 2
  • 6