0

I have following code in php page

<script type="text/javascript">
        $(document).ready(function() {
            setInterval(function () {
                $('#show').load('job-notification.php')
            }, 1000, true);


        });
    </script>

Div with ID #show displays data from job-notification.php

Every 1 sec this reloads the data from job-notification.

If i set time to '10000'(10 secs) then when page at first loads it takes 10 secs to display div #show. How can we display this on load & then every 10 secs it reloads the data from job-notification.php

Job Notifications contains recordset & data values.

Mohammed Kumel
  • 141
  • 2
  • 11

3 Answers3

4

Add an extra call to before going into setInterval

<script type="text/javascript">
    $(document).ready(function() {
    $('#show').load('job-notification.php');
        setInterval(function () {
            $('#show').load('job-notification.php')
        }, 1000, true);


    });
</script>
drew
  • 1,312
  • 8
  • 20
1

You have this:

<script type="text/javascript">
        $(document).ready(function() {
            setInterval(function () {
                $('#show').load('job-notification.php')
            }, 1000, true);


        });
    </script>

Why not just change it so that the second load inside the interval goes within the first load function, as a parameter (complete parameter):

<script type="text/javascript">
        $(document).ready(function() {
           $('#show').load('job-notification.php', setInterval(function () {
                $('#show').load('job-notification.php')
            }, 1000, true));  
        });
    </script>

This way, once the first load is done, on the complete parameter of the first load function, the set interval will be called and it will be done there and after. Documentation here: http://api.jquery.com/load/#callback-function

CodeTrooper
  • 1,890
  • 6
  • 32
  • 54
  • Which among all of this is good? I m confused? Which would be faster & better & consume less bandwidth . - @codeninja – Mohammed Kumel Apr 05 '16 at 21:08
  • They will consume the same bandwidth. Because regardless of the code, there will be the same number of `http requests`. I personally would use my way, since it's how jQuery works, by chaining stuff. Once I'm done with the first call, there's a parameter called complete which is optional, inside that parameter you can add a callback function, meaning you can execute a piece of code that will do something, in this case, the `setInterval`. – CodeTrooper Apr 05 '16 at 22:40
1

If I interpret this correctly, you want to call the function on page load and then every 10 seconds. To achieve this you could first define the function, call it, and then setup the interval. Like this:

<script type="text/javascript">
    $(document).ready(function() {
        function loadNotifications() {
            $('#show').load('job-notification.php');
        }
        loadNotifications();
        setInterval(
            function() {
                loadNotifications();
            },
            1000,
            true
        );
    }
</script>

With this method you won't have any code duplicates.

Parviz Karimli
  • 1,247
  • 1
  • 14
  • 26