Martynas Jakas
Martynas Jakas

Reputation: 89

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?

Upvotes: 0

Views: 331

Answers (2)

Bala
Bala

Reputation: 963

Try this

<script>
        $(document).ready(function(){
            setTimeout(function(){
                    loadNotification();
                    var auto_refresh = setInterval(function() {
                            loadNotification();
                    }, 10000);
            }, 1000);
        });

       function loadNotification() {
              $("#randomtext").load("notification.php");
        }

</script>

if you declare auto_refresh outside setTimeout, next load call will go in 9 seconds.

Upvotes: 1

void
void

Reputation: 36703

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>

Upvotes: 5

Related Questions