1

I need help in delaying the execution of my javascript.(not making the javascript to execute right after the webpage is being loaded) I wish to execute the javascript only after 10s after the webpage is being loaded. How can I do that? This is the script.

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

Note: I want to delay the following execution.

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

    current_index = index;
}

I tried inserting

    var delay = 10000;

or

    var interval = 10000;

none of them seem to work.

I also tried

    setTimeout (function(); 3000);

it came out with uncaught syntax error. Please Help me guys!

Note: I'm new to js/php coding...

1
  • Why are you using php here? Commented Dec 11, 2016 at 10:17

2 Answers 2

1

Looking at your code, I think you should just remove the line

show_next_live_notify();

at the bottom of your script. It automatically executes everything right upon start instead of letting setInterval do its job

To delay the whole script, replace the last two lines in the jQuery call with something like this:

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

setTimeout(startMe, 10000);
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks. But I would like to delay the script instead of making it executes right upon the webpage is reloaded. i wish to delay it by 10s. The whole script.
Thats exactly what my suggestion would do. But If you also want the setInterval to be delayed for whatever reason, you should remove the last line of your code (interval = setInterval(show_next_live_notify, (showtime + <?php print $s + 100; ?>)); and replace it with something like this
function startMe() {
0

You function name is show_live_feed_notify, and you tried to use setTimeout. Therefore I suggest you to try the following:

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

2 Comments

I tried the code. unfortunately, the javascript still execute once the webpage is loaded. I wish to delay them by 10s. How can i do that?
3000 is here the number of ms, so it's 3 seconds. It's probably because of the short delay that you think it's executed immediately on page loading. If you need 10 seconds then try the value 10000

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.