简体   繁体   English

Javascript代码未在函数结束前执行

[英]Javascript code not executing before the end of function

My project is to make a web application that graphically displays the different stages of the Prim and Dijkstra algorithm.我的项目是制作一个 Web 应用程序,以图形方式显示 Prim 和 Dijkstra 算法的不同阶段。 This is made by displaying a graph and the queue of priority, and changing nodes color if they are in the queue, visited, or currently being visited.这是通过显示图表和优先级队列来实现的,如果节点在队列中、已访问或当前正在访问,则更改节点颜色。 The priority queue is displayed with a table that contain the nodes in the queue.优先级队列与包含队列中节点的表一起显示。 I use JavaScript and Sigma JS to display the graph.我使用 JavaScript 和 Sigma JS 来显示图表。

My problem is that the code to refresh the graph and the priority queue only execute at the end of the function they are being called.我的问题是刷新图形和优先级队列的代码仅在它们被调用的函数结束时执行。 When I use debugger and stop at the critical function, everything displays right.当我使用调试器并在关键功能处停止时,一切都显示正确。 But when I don't use the debugger, the parts that refresh the graph and update the visual priority queue only execute at the end of the Dijkstra() function.但是当我不使用调试器时,刷新图形和更新视觉优先级队列的部分仅在 Dijkstra() 函数的末尾执行。 The sleep function is not the one posing problem, with or without it there is the same problem.睡眠功能不是唯一的问题,有没有它都有同样的问题。

Thanks for your help谢谢你的帮助

script.js脚本.js

var graphe = new WeightedGraph();
...
function onStartClick(){
    if(radio === 'p') graphe.Prim();
    if(radio === 'd') graphe.Dijkstra(0);
}
...
function createQueue(queue){
    var a = '<table id="queueTable"><tbody><tr>';

    for(elem in queue){
        var name = Object.keys(graphe.adjacencyList)[queue[elem].element]
        a+= "<td>"+name+"</td>";
    }
    a+="</tr><tr>"
    for(elem in queue){
        a+= "<td>"+queue[elem].priority+"</td>";
    }

    a+="</tr></tbody></table>";
    var qc = document.getElementById("queue-container");
    console.log(qc);
    qc.innerHTML = a;
}
...

algos.js算法.js

class WeightedGraph 
{
...
      sleep(ms) {
        var start = new Date().getTime(),
          expire = start + ms;
        while (new Date().getTime() < expire) {}
        return;
      }

    Dijkstra(start){
        this.tabVisited = [];
        this.tabRencontred = [];

        for(var i = 0; i < Object.keys(this.adjacencyList).length; i++){
            this.tabVisited.push(false);
            this.tabRencontred.push(false);
        }
        this.visiterPriorite(start);
    }

    visiterPriorite(sommetCourant) {
        if(!this.tabVisited[sommetCourant])
        {
            var priorityQueue = new PriorityQueue();
            priorityQueue.enqueue(sommetCourant, 0); // ->
            var csChar = Object.keys(this.adjacencyList)[sommetCourant];

            // Not executing till the end of the function
            s.graph.nodes().forEach(n => {
                if(n.id === csChar)
                {
                    n.color = "grey";
                }
            });
            s.refresh();
            createQueue(priorityQueue.items)

            this.sleep(500);

            while(!priorityQueue.isEmpty())
            {
                ...
                // code posing problem is called again here
                ...
            }
        }
    }
...
}    



I solved my problem by setting the function visiterPriorite to async : async visiterPriorite(sommetCourant)我通过将函数 visiterPriorite 设置为 async 解决了我的问题: async visiterPriorite(sommetCourant)

and changing my sleep method to the one below并将我的睡眠方法更改为下面的方法

async sleep(ms) 
{
    return new Promise((resolve, reject) => {setTimeout(resolve, ms)});
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM