PHP js函数的时间间隔


php js function time interval

我有这个函数:

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

它每10秒加载一次它从notification.php获得的内容到我的div id randomtext。有没有可能让它在加载页面后1秒内第一次运行,然后每10秒运行一次?

是可能的,你只需要在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>

试试这个

<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秒后进行。