JavaScript - 延迟执行某些 javascript

JavaScript - Delay execution of certain javascript

我需要帮助来延迟 javascript 的执行。(不是让 javascript 在网页加载后立即执行)我只想执行 javascript加载网页后 10 秒后。我怎样才能做到这一点?这是脚本。

    <script>
    var interval = 10000;
    var current_index = -1;
    var sales_feeds = [];
    var showtime = 5000;
        <?php $s = get_option('wc_feed_delay_between_popups_appear');
        if (!$s) { 

          $s = 5000;
        }
        ?>
    function hide_prev_feed_notify(index)
    {
        if( sales_feeds.eq(current_index).length > 0 )
        {
            sales_feeds.eq(current_index).animate({bottom: '-90px'}, 500);
        }
    }
    function show_live_feed_notify(index)
    {
        sales_feeds.eq(index).animate({bottom: '10px'}, 1000);

        current_index = index;
    }
    function show_next_live_notify()
    {
        if( (current_index + 1) >= sales_feeds.length )
        {
            current_index = -1;
        }

        //add randomness 
        current_index = (Math.floor(Math.random() * (sales_feeds.length + 1))) - 1;;

        if( window.console )
            console.log('will show ' + (current_index+1));

          show_live_feed_notify(current_index + 1);
          setTimeout(function() { hide_prev_feed_notify(current_index + 1); }, showtime);
    }
    function stop_live_notify()
    {
        removeInterval(inverval);
    }
    function readCookie(name) 
    {
        var nameEQ = escape(name) + "=";
        var ca = document.cookie.split(';');
        for (var i = 0; i < ca.length; i++) 
        {
            var c = ca[i];
            while (c.charAt(0) === ' ') c = c.substring(1, c.length);
            if (c.indexOf(nameEQ) === 0) return unescape(c.substring(nameEQ.length, c.length));
        }
        return null;
    }
    jQuery(function()
    {
        jQuery('.wc_feed_close_btn').click(function()
        {
            var days = 30;
            var date = new Date();
            date.setTime(date.getTime() + (days *24 *60 *60 *1000));
            if(window.console)
                console.log(date.toGMTString());
            document.cookie = 'wc_feed_closed=true; expires=' + date.toGMTString() + ';';
            jQuery('.live-sale-notify').css('display', 'none');
            clearInterval(interval);
            return false;
        });

        sales_feeds = jQuery('.live-sale-notify');

        show_next_live_notify();
        interval = setInterval(show_next_live_notify, (showtime + <?php print $s + 100; ?>));
    });
    </script>

注意:我想延迟执行下面的内容。

    function show_live_feed_notify(index)
{
    sales_feeds.eq(index).animate({bottom: '10px'}, 1000);

    current_index = index;
}

我试过插入

    var delay = 10000;

    var interval = 10000;

none 似乎有效。

我也试过了

    setTimeout (function(); 3000);

它出现了未捕获的语法错误。 请帮帮我!

注意:我是 js/php 编码新手...

您的函数名称是 show_live_feed_notify,并且您尝试使用 setTimeout。因此,我建议您尝试以下操作:

var delay = 10000; // 10 seconds
setTimeout(function() {
    show_live_feed_notify(current_index + 1);
}, delay )

查看您的代码,我认为您应该删除行

show_next_live_notify();

在脚本的底部。它会在启动时自动执行所有内容,而不是让 setInterval 完成它的工作

要延迟整个脚本,请将 jQuery 调用中的最后两行替换为如下内容:

function startMe() {
   interval = setInterval(show_next_live_notify, (showtime + <?php print $s + 100; ?>));
}

setTimeout(startMe, 10000);