JavaScript可以';提示后不要画图

JavaScript可以';提示后不要画图,javascript,animation,setinterval,prompt,Javascript,Animation,Setinterval,Prompt,我通过调用setInterval(draw,interval)使用HTML画布和javascript制作动画。间隔设置为10(我认为是10英里,但我不确定!),下面是绘制方法: function draw() { clear(); for(var i=0;i<nodes.length;i++){ for(var j=0;j<nodes[i].length;j++){ drawNode(nodes[i][j]);

我通过调用
setInterval(draw,interval)
使用HTML画布和javascript制作动画。间隔设置为10(我认为是10英里,但我不确定!),下面是绘制方法:

function draw()
{
    clear();
    for(var i=0;i<nodes.length;i++){
        for(var j=0;j<nodes[i].length;j++){
            drawNode(nodes[i][j]);
        }
    }
}

function clear(){
    context.clearRect(0,0,WIDTH,HEIGHT);
}

function drawNode(node){
     var x=node.getX();
     var y=node.getY();
     ctx.lineWidth=5;
     ctx.strokeStyle='black';
     ctx.strokeRect(x,y,nodeWidth,nodeHeight);
     if(node.isSource()&&sinkNotFound) ctx.fillStyle='yellow';
     else if(node.isSource()&&!sinkNotFound) ctx.fillStyle='orange';
     else if(!node.isTraversable()) ctx.fillStyle='black';
     else if (node.isOpen()) ctx.fillStyle='green';
     else if(node.isDiscovered()) ctx.fillStyle='red';
     else ctx.fillStyle='white';
     ctx.fillRect(x+ctx.lineWidth,y+ctx.lineWidth,
                  nodeWidth-2*ctx.lineWidth,nodeHeight-2*ctx.lineWidth);
}
draw方法在单击按钮之前,按我的需要每隔10次调用一次。但是如果我点击按钮,那么在我将信息输入两个提示框之后,draw方法将不再被调用。我不知道如何确保draw方法在给出正确的输入后继续被调用。需要说明的是,draw方法不依赖于
状态
变量。
state
变量仅用于允许用户单击setSource按钮,我只想允许每次使用一次


谢谢你能提供的任何帮助

您确定draw()不再被调用吗?可能askForSource()中的某些内容修改了
节点
数组?在draw()中放入一个console.log语句,该语句只打印时间戳或其他内容。也许可以尝试在asForSource()的末尾调用draw。顺便说一句,setInterval中的10是10毫秒,但您应该使用window.requestAnimationFrame(draw),最后一个只调用下一个函数,如果它可以在屏幕上绘制某些内容。我确信draw()是在通过提示框输入后,不会调用。我实际上使用的是
console.log('drawing')
查看何时调用draw()方法。它是在输入之前调用的,但不是在输入之后调用的。此外,在askForSource()末尾调用draw()也不能解决此问题:(我以前听说过使用window.requestAnimationFrame(draw)是一种更有效的动画方法,但我不知道如何使用它,坦率地说,我现在没有时间学习。
function askForSource(){
    var goodInput = false;
    while (!goodInput) {
        if (state == 0) {
            var x = prompt("Source x: ", "x gride squares from the left");
            var y = prompt("Source y: ", "y gride squares from top");
            if (nodes[y][x].isTraversable()) {
                console.log('good source');
                state = 1;
                nodes[y][x].makeSource();
            }
            else alert('Invalid source');
        }
    }
}