【问题标题】:I need this jquery/ajax script to run every 30 seconds [duplicate]我需要这个 jquery/ajax 脚本每 30 秒运行一次 [重复]
【发布时间】:2013-06-06 18:50:32
【问题描述】:

现在我已经困惑了一段时间了。 我在 jquery/ajax 中得到了一部分:

<script type="text/javascript">
$(document).ready(function(){
    jQuery.ajax({
        type: "GET",
        url: "sessioncheck.php",
        dataType:"json",
        success:function(response){
            if (response) {
                window.location.href = 'logout.php';
            }
            else {
                // Process the expected results...
            }
        }

    });
});
</script>

这非常有效,但我希望此过程每 30 秒重复一次。 有人可以帮我吗?我读过一些关于 setinterval 的文章,但我似乎无法让它发挥作用。

非常感谢您的所有帮助!

【问题讨论】:

  • 为什么?如果您正在进行会话管理,服务器应该已经能够通过比较会话上的最后活动时间来处理此问题,您不需要客户端每 X 秒 ping 服务器一次。
  • 因为如果他们从另一个位置登录,我希望旧会话自动注销。换句话说,他们只能登录一次。
  • 如果它很重要,你应该在服务器端进行,因为 javascript 很容易被禁用。
  • 该站点使用流服务,如果没有 javascript,流将不会加载。所以这不是一个真正的问题。虽然做这个服务器端会更好,但我想不出一种方法来做到这一点..

标签: php javascript jquery ajax


【解决方案1】:

只需做一个 setInterval...

  <script type="text/javascript">
   $(document).ready(function(){
   setInterval(function(){
    jQuery.ajax({
        type: "GET",
        url: "sessioncheck.php",
        dataType:"json",
        success:function(response){
            if (response) {
                window.location.href = 'logout.php';
            }
            else {
                // Process the expected results...
              }
           }
        });
    }, 30000);
  });
  </script>

【讨论】:

  • 老鼠……我打字太慢了……哈哈
  • 这只会在 30 秒内运行一次......不是每 30 秒一次
【解决方案2】:

像这样添加一个间隔。

<script type="text/javascript">
$(document).ready(function(){
    setInterval(function(){
        jQuery.ajax({
            type: "GET",
            url: "sessioncheck.php",
            dataType:"json",
            success:function(response){
                if (response) {
                    window.location.href = 'logout.php';
                }
                else {
                    // Process the expected results...
                }
            }
        });
    }, 30000);
});
</script>

更多信息在这里:http://www.jquery4u.com/jquery-functions/setinterval-example/

【讨论】:

    【解决方案3】:
    $(document).ready(function(){
        setInterval(function() {
            jQuery.ajax({
                type: "GET",
                url: "sessioncheck.php",
                dataType:"json",
                success:function(response){
                    if (response) {
                        window.location.href = 'logout.php';
                    }
                    else {
                        // Process the expected results...
                    }
                }
    
            });
        }, 30000);
    });
    

    【讨论】:

    • 谢谢!完美运行!
    • 需要注意的是,该方法会每隔30秒发起一次请求,但是响应可能会延迟甚至乱序返回。
    • @GeorgeCummins 如果服务器响应时间超过 30 秒,他可能有更大的问题需要担心。
    • @GeorgeCummins 然后在下一次迭代中取消之前的请求。
    猜你喜欢
    • 2022-12-18
    • 2012-03-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-24
    • 1970-01-01
    • 2012-06-13
    相关资源
    最近更新 更多