0

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?

2 Answers 2

5

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>
Sign up to request clarification or add additional context in comments.

4 Comments

damn it. you made it look easy. Thanks though. I am still beginner in js
need to wait 10 minutes :y
Hey there again. I tried your code, mine again, some testing and now for some reason simple $("#randomtext").load("notification.php") doesn't work what the hell. notification.php exists and I checked it with file_exists();
agh I fixed it somehow. Thank you !
1

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.

1 Comment

This is very nice too ! Thanks !

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.