Repeat code every 4 seconds

后端 未结 6 2078
遇见更好的自我
遇见更好的自我 2021-01-04 04:07

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         


        
6条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2021-01-04 04:43

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

提交回复
热议问题