在setInterval函数不更新随机数 [英] Random number not updating in SetInterval function

查看:178
本文介绍了在setInterval函数不更新随机数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试过在SO发现了一些不同的方法来此只是不能似乎得到它的工作。

I've tried a few different approaches to this found on SO but just cant seem to get it to work.

我试图在数组来更新随机选择的项目,在一个setInterval函数,但是随机数不改变

I'm trying to update the randomly selected item in an array, in a setInterval function, but the random number is not changing.

据随机选择第一次负载,但那么它不事后更新,每次函数再次运行。

It is randomly chosen on first load, but then it doesn't update after the fact, each time the function runs again.

这是所有使用lazylinepainter插件: https://github.com/camoconnell/lazy-行画家

This is all using the lazylinepainter plugin: https://github.com/camoconnell/lazy-line-painter

var pathArray = [pathOne,pathTwo,pathThree,pathFour,pathFive,pathSix],
colors = ['#e51616','#0000FF','#FFFF00','#00FF00'],
drawBox = $('#drawing-box'),
svg = $('#drawing-box svg'),
svgPath = document.createElementNS("http://www.w3.org/2000/svg", "path");

function randomFrom(array) {
   return array[Math.floor(Math.random() * array.length)];
}

randomColor = randomFrom(colors);

var i = Math.floor(Math.random() * (5 - 0 + 1));
console.log(i);

function Draw(){

   var drawLoop = setTimeout(function (){
                    $('#drawing-box svg path').animate({'opacity':0},300);
                    setTimeout(function(){
                        $('#drawing-box svg path').remove();
                        drawBox.lazylinepainter('paint');
                        console.log(pathArray[i]);
                    },350);
                },5500);

   var drawFunc = drawBox.lazylinepainter({
                "svgData": pathArray[i],
                "strokeColor": randomColor,
                "strokeWidth": 5,
                "responsive": true,
                "onComplete": drawLoop
            });

   drawFunc.lazylinepainter('paint')
};

setInterval(function(){
   Draw();
},6000);

这是的jsfiddle -----

重新运行小提琴遍地看到随意选择不同的路径(因为它没有其他更新)。

Rerun the fiddle over and over to see the different paths chosen at random (since its not updating otherwise).

希望这片段是明确的,仍试图在那里一些不同的东西。

Hopefully that snippet is clear, was still trying a few different things in there.

的最终目标是让该行从pathArray(pathOne,pathTwo,pathThree,等等,等等),每个区间画一个随机选择的项目。

The ultimate goal is to have that line draw a randomly chosen item from the pathArray (pathOne, pathTwo, pathThree, etc. etc.) with each interval.

推荐答案

在我看来,你只有一次调用实际的功能,然后将其分配给 randomColor 这被使用一遍又一遍。

Seems to me you are calling the actual function only once and then assigning it to randomColor which gets used over and over.

你应该做的反而是把它需要的地方:

What you should do instead is call it where it is needed:

var drawFunc = drawBox.lazylinepainter({
            "svgData": pathArray[i],
            "strokeColor": randomFrom(colors),
            "strokeWidth": 5,
            "responsive": true,
            "onComplete": drawLoop
        });

这样,你得到一个新鲜的每一次。

That way you get a fresh one every time.

这篇关于在setInterval函数不更新随机数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆