JavaScript - 延迟某些javascript的执行

时间:2016-12-11 10:08:18

标签: javascript php jquery delayed-execution

我需要帮助推迟执行我的javascript。(不要在加载网页后立即执行javascript)我希望在加载网页后10s后才执行javascript。我怎样才能做到这一点?这是脚本。

    <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;

它们似乎都不起作用。

我也试过

    setTimeout (function(); 3000);

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

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

2 个答案:

答案 0 :(得分:1)

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

show_next_live_notify();

位于脚本底部。它会在启动时自动执行所有操作,而不是让setInterval完成其工作

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

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

setTimeout(startMe, 10000);

答案 1 :(得分:0)

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

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