Martynas Jakas Martynas Jakas - 1 month ago 7
PHP Question

php js function time interval

I have this function:

<script>
var auto_refresh = setInterval(
(function () {
$("#randomtext").load("notification.php");
}), 10000);
</script>




it loads what it gets from notification.php to my div with id randomtext every 10 seconds. Is it possible to make it run at the very first time in like 1 second after the page is loaded and then every 10 seconds?

Answer

Yes it is possible, you just need to call .load after 1 second

<script>
    // Run it for the first time after 1 second
    setTimeout(function(){
       $("#randomtext").load("notification.php");
    }, 1000);

    // Run it every ten seconds
    var auto_refresh = setInterval(
         (function () {
                 $("#randomtext").load("notification.php");
         }), 10000);
</script>
Comments