首页 > 解决方案 > 即使满足条件,for循环也不会执行

问题描述

该代码应该绘制从我制作的几个字段中获取值的图形,一切正常,直到它进入 for 循环,由于某种原因,循环被完全跳过,我在循环中放入了一个 alert() 以查看是否我的命令的语法不正确,但它没有引发警报,好像 for 循环甚至没有运行,但是它应该运行,因为条件为真,那么是什么导致代码跳过?

   var canvas = document.getElementById("myCanvas");
        var ctx = canvas.getContext("2d");
        var dN = document.getElementsByName("dName");
        var dV = document.getElementsByName("dValue");
        var titleInput = document.getElementById("titleInput");
        var stitleInput = document.getElementById("stitleInput");



        function blek()    {

            var propRatio = 1;
            dV.forEach(ratioCalc);

            function ratioCalc(val){
                    if(propRatio > (1 / (val.value / 1000))) {
                    propRatio = (1 / (val.value / 1000)) / 100 * 70;
                    }
            }

            ctx.font = "30px 'Roboto Mono'";
            ctx.clearRect(0,0, canvas.width, canvas.height);
            ctx.fillText(titleInput.value, 10, 50);
            ctx.font = "24px 'Roboto Mono'";
            ctx.fillText(stitleInput.value, 10, 80);

            for (i = 0; i > dV.length; i++) {
                //This is problem part, the loop doesn't execute at all and there is no console output.
                ctx.fillText(dN[i].value, 10, 133 + (60 * (i+1)));
                ctx.fillRect(200, 100 + (50 * (i+1)) + 10, dV[i].value * propRatio, 50);
                ctx.fillText(dV[i].value, 20 + dV[i].value * propRatio, 133 + (50 * (i+1) + 10));

            }


        }

标签: javascriptloopsfor-loop

解决方案


推荐阅读