In jQuery how to create an alert every 5 seconds?

Can I do this using jQuery or do I need to use something else?

+3
source share
4 answers

The function is jQuery.delay()designed to delay the execution of functions in the jQuery queue. There is already a built-in Javascript function for handling interval functions, you would not use jQuery to achieve this.

setInterval(function() {
    alert("Message to alert every 5 seconds");
}, 5000);
+16
source

Again, there is no need for jQuery:

setInterval(function() { 
   alert("How Now Brown Cow");
}, 5000);

(Don't tell anyone, but @Justin Niessner is right, jQuery is JavaScript)

+4
source

setInterval() setTimeout(). jQuery.

setInterval() 2 - . , clearInterval(intervalID). , setInterval(), .

setTimeout() , . , , setTimeout() setTimeout().

, , setInterval() - , setInterval() .

+2

script Cookbook/wait , :

(function($){

   $.fn.runItIf = function(ex, fn, args) {

      var self = this;

      return $(self).queue(function() {

          args = args || [];
          if (ex)
             fn.apply.(self, args );

          $(self).dequeue();
      });
   };

   $.fn.runIt = function(fn, args) {

     return $(this).runItIf(true, fn, args);

   };

   $.fn.wait = function(time, type) {

        time = time || 1000;
        type = type || "fx";

        return this.queue(type, function() {
            var self = this;
            setTimeout(function() {
                $(self).dequeue();
            }, time);
        });
    };

})(jQuery); 

An example based on an example in Cookbook / wait :

   function runIt() {

      var expression = true;

      $("div").wait()
              .animate({left:'+=200'},2000)
              .runIt(function() { /* ... */ } ).wait(10)
              .runItIf(expression, function() { /* ... */ } ).wait(10)
              .animate({left:'-=200'},1500,runIt);
   }
   runIt();
+1
source

Source: https://habr.com/ru/post/1784634/


All Articles