php js函数时间间隔

时间:2016-11-01 10:22:56

标签: javascript php

我有这个功能:

<script>
    var auto_refresh = setInterval(
         (function () {
                 $("#randomtext").load("notification.php");
         }), 10000);
</script>

它每隔10秒从notification.php加载到id为randomtext的div。是否有可能在页面加载后的1秒内第一次运行,然后每10秒运行一次?

2 个答案:

答案 0 :(得分:5)

是的,有可能,你只需要在1秒后拨打.load

<script>
    // Run it for the first time after 1 second
    setTimeout(function(){
       $("#randomtext").load("notification.php");
    }, 1000);

    // Run it every ten seconds
    var auto_refresh = setInterval(
         (function () {
                 $("#randomtext").load("notification.php");
         }), 10000);
</script>

答案 1 :(得分:1)

试试这个

<script>
        $(document).ready(function(){
            setTimeout(function(){
                    loadNotification();
                    var auto_refresh = setInterval(function() {
                            loadNotification();
                    }, 10000);
            }, 1000);
        });

       function loadNotification() {
              $("#randomtext").load("notification.php");
        }

</script>

如果在setTimeout之外声明 auto_refresh ,则下次加载调用将在9秒内完成。