首页 > 解决方案 > 同步超时问题(Javascript 和 ajax)

问题描述

我正在制作一个超时系统,在一定时间后注销使用,用户打开标签浏览器。我不想在用户使用系统时取代他。为此,我正在检查他何时移动鼠标并重置时间。问题发生在那里,如果它在一个选项卡中,那就完美了。但是,当在 2 个选项卡步骤中,会话结束并且不会向用户显示消息。**本地存储**更改正确,但在另一个选项卡上,它继续运行。我想,当本地存储为 0 时,强制 ajax 结束会话。** 跟随代码 **

SetTimeOut:
        function () {
            //1800000
            var tempo = 10000;
            localStorage.setItem("tempo", "1");
            var timeout= 0;
            var x = new Boolean(false);

            function EventoAlert() {
                swal({
                    title: "Sessão expirada",
                    text: "Sessão expirada por inatividade",
                    icon: "dist/img/time-clock.webp",
                    button: true,
                    button: "Sair",
                    className: "swalsair",
                    closeOnClickOutside: false,
                }).then((value) => {
                    var url_ajax = 'index.php';
                    window.location.href = url_ajax;
                }).catch(swal.noop);
            };

            $(function () {
                timeout = setTimeout(function () { }, tempo);
            });


            $.ajax({
                type: 'POST',
                url: 'index.php?id=98',
                data: '236464302',
                success:
                 function () {
                        $(document).on('mousemove', async function () {
                                if(localStorage.getItem("tempo") == "0" && !x){
                                    $.ajax({
                                        type: 'POST',
                                        url: 'index.php?id=59&func=timeout',
                                        data: '236464302',
                                        success: function () {
                                            EventoAlert();
                                        },
                                        error: function () {
                                            window.location.href = 'index.php';
                                        }
                                    });
                                }
                            if (timeout !== null) {
                                clearTimeout(timeout);
                                timeout = setTimeout(function () { }, tempo);
                            }
                            console.log("testeeeeeee");
                            timeout = setTimeout(function () {
                                $.ajax({
                                    type: 'POST',
                                    url: 'index.php?id=59&func=timeout',
                                    data: '236464302',
                                    success: function () {
                                        localStorage.setItem("tempo", "0");
                                        x = true;
                                        EventoAlert();
                                    },
                                    error: function () {
                                        window.location.href = 'index.php';
                                    }
                                });
                            }, tempo);

                        });

                    },
                error: function () {
                    window.location.href = 'index.php';
                }
            });

        },

标签: javascriptsessionsynchronizationtimeoutlocal-storage

解决方案


要检查用户何时移动鼠标,您需要在代码中输入 ctrl + c :

var secondsUntilStop = [your time here];
var s;
window.onload = function(){
  setInterval(check, 1000);
}

function check(){
  s++
  if(s >= secondsUntilStop){
    //your program stop code here
  }
}

推荐阅读