javascriptajaxdatetimepicker

setInterval with an ajax request is auto closing DateTimePicker


I have this code below:

setInterval(function(){
                $.ajax({
                    url: "{{ route('logout_checker')}}",
                    type: "GET",
                    success: function(data){
                        if( data == 0 ){
                            location.reload();
                        }
                    }
                });
            }, 1000);

My problem is that it will auto-close the datetime picker (https://nehakadam.github.io/DateTimePicker/)

the picker will pop up and then close so you can't select anything.

is there a better way of handling the ajax request since I'm using that to check if there is still an active session and if there's none it will reload the page so it will go to the login page.

If I remove the ajax request the datetime picker works normally. it's only when the ajax request is present inside the setInterval that the problem occurs.


Solution

  • found the fix. I changed from using ajax to fetch.

    I'm still not sure why or how this works though but it does. so if anyone can enlighten me about it. it will be highly appreciated.

    setInterval(function(){
            fetch("{{ route('logout_checker') }}")
                .then(response => response.text())
                .then(data => {
                    if( data == '0' || data == '' ){
                        location.reload();
                    }
                });
        }, 1000);