1

I have setInterval on my script. But when I enter to another tab of browser or use another app, setInterval stopping. When I enter my tab I written my script it continues working.

Here is my code with setInterval:

var interval = window.setInterval(() => {
                $('#notifications-1').load('{{ route('get.loader.notification') }}');
                $('#notifications-2').load('{{ route('get.accepts') }}');

                if (parseInt($.ajax({
                        type: "GET",
                        url: '{{ route('check-active-order') }}',
                        async: false
                    }).responseText) > 0) {

                    if (navigator.geolocation) {
                        navigator.geolocation.getCurrentPosition(setPosition);
                    } else {
                        alert("Geolocation is not supported by this browser.");
                        clearInterval(interval);
                    }

                }
            }, 1000);

            function setPosition(position) {
                var setdata = $.ajax({
                        type: "GET",
                        url: '{{ route('save-carrier-location') }}?latitude='+position.coords.latitude+'&longitude='+position.coords.longitude,
                        async: false
                    }).responseText;
            }

here is my code when i tried setTimeout

function timer() {

                if (parseInt($.ajax({
                        type: "GET",
                        url: '{{ route('check-active-order') }}',
                        async: false
                    }).responseText) > 0) {

                    if (navigator.geolocation) {
                        navigator.geolocation.getCurrentPosition(function(position) {
                            var setdata = $.ajax({
                                type: "GET",
                                url: '{{ route('save-carrier-location') }}?latitude=' + position.coords
                                    .latitude + '&longitude=' + position.coords.longitude,
                                async: false
                            }).responseText;
                        });
                    } else {
                        alert("Geolocation is not supported by this browser.");
                    }

                }

                setTimeout(timer(), 1000);
            }

            timer();

Please help me, if is there any way!

I tried setTimeout instead setInterval by calling function itself. But not worked. Actually console.log result is working when I use setTimeout but my ajax code is not continueing.

0 Answers0