【问题标题】:pass a random generated variable to setInterval将随机生成的变量传递给 setInterval
【发布时间】:2016-02-29 17:12:33
【问题描述】:

我使用 setInterval 为动态创建的元素设置了动画。单击元素时,我希望它更改为随机方向(左、右、上或下)。

认为问题是我不能将随机创建的方向传递给 setInterval 函数。

这里的html....

<div id="klicker"></div>

这里的 JavaScript .....

var box = document.getElementById("klicker");
var x = 1;
var startp = 200;
box.onclick = function() {
    var newdiv = document.createElement('div');
    document.body.appendChild(newdiv);
    newdiv.style.width = 200 + 'px';
    newdiv.style.height = 200 + 'px';
    newdiv.style.backgroundColor = 'grey';
    newdiv.style.position = 'absolute';
    newdiv.style.left = startp + "px";
    newdiv.style.top = startp + "px";
    newdiv.setAttribute('id', 'runner')
    var run = document.getElementById('runner');
    var dire = 'right';
    run.onclick = function() {
        var mov = function() {
            randoms();
        };
        function randoms() {
            alert('ddddd');
            var rnd = (Math.random() * 4) + 1; //ranom number 1-4
            var rnd_down = Math.floor(rnd); //make it en integer
            /*--- make each number a uniqe direction---*/
            if (rnd_down == 1) {
                dire = 'right';
            } else if (rnd_down == 2) {
                dire = 'left';
            } else if (rnd_down == 3) {
                dire = 'up';
            } else {
                dire = 'down';
            }
        };

        var moveit = setInterval(function() {
            movefunc();
        }, 50);

        function movefunc() {
            /*----each direction will make the div travel in diffrent directions--*/
            if (dire = 'right') {
                x = x + 1;
                newdiv.style.left = x + 'px';
            } else if (dire = 'left') {
                x = x - 1;
                newdiv.style.left = x + 'px';
            } else if (dire = 'up') {
                x = x - 1;
                newdiv.style.top = x + 'px';
            } else {
                x = x + 1;
                newdiv.style.top = x + 'px';
            }
        };
    };
}

【问题讨论】:

  • var dire 范围仅在 box.onclick 处理程序内。它在movefunc 中不可用。
  • 第二次点击box会生成一个带有重复id = 'runner'的div。
  • var newdiv 范围仅在 box.onclick 处理程序内。它在movefunc 中不可用。

标签: javascript random setinterval


【解决方案1】:
function movefunc() {
            /*----each direction will make the div travel in diffrent directions--*/
            if (dire = 'right') {
                x = x + 1;
                newdiv.style.left = x + 'px';
            } else if (dire = 'left') {
                x = x - 1;
                newdiv.style.left = x + 'px';
            } else if (dire = 'up') {
                x = x - 1;
                newdiv.style.top = x + 'px';
            } else {
                x = x + 1;
                newdiv.style.top = x + 'px';
            }
        };

Rookie 错误 :) (即使它是 most common error in programming 之一,请参阅 #4 )在您的情况下,您应该使用 == 而不是 = (用于分配,== 用于比较 )。我什至建议您使用===,它也会比较类型。 (1 == '1' 将返回 true1 === '1' 将返回 false 因为 1 是 integer 类型,而 '1' 是 char 类型)

【讨论】:

  • 他应该使用尤达风格:'left' == dire。必须给他更正的代码 sn-p。
  • if(dire.length &gt; 0 &amp;&amp; 'left' === dire) 最好先检查一下 dire 是否为空。三等号。程序员偏执的习惯必须传递给新的学徒。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-06-11
  • 1970-01-01
  • 1970-01-01
  • 2015-01-26
  • 1970-01-01
  • 2020-06-29
  • 2017-06-12
相关资源
最近更新 更多