Search code examples
phpjqueryfunctioncall

Auto call a php function every 10 seconds


I have a function that reads certain elements from a file that is updated nearly every second. I am trying to find a way to auto call the function so it re-prints that information every ten seconds.

What is the best way to do this? I tried some javascript but I could not get it to work. All help is appreciated.


Solution

  • You just need to put it in a function and call it again when it's finish.

    function ajaxCall() {
       $.ajax({
          ...
       }.always(function() {
          setTimeout(ajaxCall, 10000); // 10 seconds
       });
    }
    
    ajaxCall();
    

    EDIT

    Updated due to roasted comment.