0

Here's my code

(function ($) {

        $.fn.snow2 = function (options) {

            var $flake = $('<a style="text-decoration: none;" href="/flake"><div id="flake" /></a>').css({
                'position': 'absolute',
                'top': '-50px',
                'z-index': '100'
            }).html('&#10052;'),
                documentHeight = $(document).height(),
                documentWidth = $(document).width(),
                defaults = {
                    minSize: 30,
                    maxSize: 50,
                    newOn: Math.floor(Math.random() * 14000) + 7000,
                    flakeColor: "#CE1126"
                },
                options = $.extend({}, defaults, options);


            var interval = setInterval(function () {
                var startPositionLeft = Math.random() * documentWidth - 100,
                    startOpacity = 0.5 + Math.random(),
                    sizeFlake = options.minSize + Math.random() * options.maxSize,
                    endPositionTop = documentHeight - 40,
                    endPositionLeft = startPositionLeft - 100 + Math.random() * 200,
                    durationFall = documentHeight * 10 + Math.random() * 5000;
                $flake
                    .clone()
                    .appendTo('body')
                    .css({
                        left: startPositionLeft,
                        opacity: startOpacity,
                        'font-size': sizeFlake,
                        color: options.flakeColor
                    })
                    .animate({
                            top: endPositionTop,
                            left: endPositionLeft,
                            opacity: 0.2
                        },
                        durationFall,
                        'linear',
                        function () {
                            $(this).remove()
                        }
                );
            }, options.newOn);

        };

    })(jQuery);

I am trying to get it to spit out a snow flake at a random interval like 7000 and 14000 milliseconds, the problem is when I call the function it gets a random number between 7000 and 14000 and uses that same value over and over again. So say it returns 12806, it will spit out a snow flake every 12806 milliseconds. I want a new number each time. How would I go about accomplishing this? I cannibalized this code from something kinda different and am not very experienced with JavaScript or jQuery. Any help is appreciated.

1
  • I love the snow! what a fun idea. Commented Dec 12, 2013 at 19:16

2 Answers 2

2

The problem is setInterval, which does exactly what you are complaining about. I think you'd rather use setTimeout. From the Mozilla Developer Network:

setTimeout() calls a function or executes a code snippet after specified delay.

setInterval() calls a function or executes a code snippet repeatedly, with a fixed time delay between each call to that function.

Sign up to request clarification or add additional context in comments.

Comments

1

Yes I would use setTimeout and call the function itself with a new timeout value. Also I would set newOn to a function instead of a variable since it will only be calculated once as a variable.

See if this fiddle behaves like you want: http://jsfiddle.net/e9KE9/1/

In the fiddle I set the first Timeout to 0 so that the first would appear immediately.

P.s.s this feels more like snow: http://jsfiddle.net/e9KE9/2/ Pretty cool idea!

$.fn.snow2 = function (options) {
    var $flake = $('<a style="text-decoration: none;" href="/flake"><div id="flake" /></a>').css({
        'position': 'absolute',
        'top': '-50px',
        'z-index': '100'
    }).html('&#10052;'),
    documentHeight = $(document).height(),
    documentWidth = $(document).width(),
    defaults = {
       minSize: 30,
       maxSize: 50,
       newOn: function(){return Math.floor(Math.random() * 14000) + 7000},
       flakeColor: "#CE1126"
    },
    options = $.extend({}, defaults, options);

    function newFlake() {
        var startPositionLeft = Math.random() * documentWidth - 100,
            startOpacity = 0.5 + Math.random(),
            sizeFlake = options.minSize + Math.random() * options.maxSize,
            endPositionTop = documentHeight - 40,
            endPositionLeft = startPositionLeft - 100 + Math.random() * 200,
            durationFall = documentHeight * 10 + Math.random() * 5000;
        $flake
            .clone()
            .appendTo('body')
            .css({
                left: startPositionLeft,
                opacity: startOpacity,
                'font-size': sizeFlake,
                color: options.flakeColor
            })
            .animate({
                    top: endPositionTop,
                    left: endPositionLeft,
                    opacity: 0.2
                },
                durationFall,
                'linear',
                function () {
                    $(this).remove()
                }
        );
        setTimeout(newFlake, options.newOn());
    };
    setTimeout(newFlake, options.newOn());
}(jQuery);

4 Comments

@ Chris B jsfiddle.net/e9KE9/1 Was really close but it spits out a flake as soon as it's run, how would I make it so that it waits a random time for even the first flake?
Oops sorry I had that running and then in my excitement to modify the code changed it. This one should wait the correct amount of time first. Sorry! jsfiddle.net/e9KE9/7
When I was first testing 7-21 seconds was just too long to wait :)
Thanks so much! I am using it to kinda hide an easter egg coupon code on our site and it gets randomly mixed in with other flakes and if you click on it you get a coupon code.

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.