How to run a PHP script every 10 seconds

Whilst using cron to call PHP scripts allows for intervals as low as 1 minute sometimes you may need a lower rate, here is how to call a PHP file every 10 seconds.

Using Jquery and ajax allows for calling an XHR request every set amount of milliseconds (setInterval). In this case, it is 10000 milliseconds for 10 seconds.

The script below stored at (run.html) when called will continuously run a get request to call.php until the page/connection is closed.

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script type="text/javascript">
    setInterval(function () {
        $.ajax({
            url: 'call.php',
            success: function (data) {
                console.log("Success");
            }
        });
    }, 10000);
</script>

This calls call.php every 10 seconds and will write Success in the console each time it runs.

To modify this script for some GET request parameters it looks like this:

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script type="text/javascript">
    setInterval(function () {
        $.ajax({
            url: 'call.php',
            data: {id: 44, key: 'XJF4YS1JF6H'},
            success: function (data) {
                console.log("Success");
            }
        });
    }, 10000);
</script>

This will call call.php?id=44&key=XJF4YS1JF6H every 10 seconds.

Of course you can change the 10-second interval to 5, 15, 30 etc just remember that it is in milliseconds.