为什么我的函数没有被调用

Why is my function not being called?

本文关键字:调用 我的 函数 为什么      更新时间:2023-09-26

我正在编写一个脚本,它基本上将数据库加载为表。这只是它的基本版本,但应该涵盖我试图做的一切。

HTML:

<a href="#" onclick="kiloseqResult=dbload('sample_kiloseq')">Load database</a>
<div id="result_table" style="visibility:hidden;">
    <center>
        <table border="0" cellspacing="3" cellpadding="3" id="summaryTable" class="table table-striped tablesorter">
            <thead>
                <tr style="font-weight: bold; text-align: center;">
                    <th>Well ID</th>
                    <th>Dominant Gene</th>
                    <th>%</th>
                    <th>Secondary Gene</th>
                    <th>%</th>
                    <th>No. of Reads that Mapped</th>
                    <th>No. of Mutations</th>
                    <th>Mutation Information</th>
                    <th>View</th>
                </tr>
            </thead>
            <tbody id="summaryBody">
            </tbody>
        </table>
</div>

Javascript:

var kiloseqResult
function dbload(name){
    var r = new XMLHttpRequest();
    r.open("GET", "/db/"+name, true);
    r.onreadystatechange = function () {
        if (r.readyState != 4 || r.status != 200) return;
        kiloseqResult = r.responseText; 
        console.log(kiloseqResult)
        return kiloseqResult
        structureTable();
    };
    r.send()
}
function structureTable(){
    if (kiloseqResult==null){
        throw "Error: no databse defined"
    };
    document.getElementById("summaryTable").style.visibility="visible";
    kiloseqDatabase = JSON.parse(kiloseqResult);
    var table = document.getElementById("summaryBody");
    for (i=0;i<kiloseqDatabase.length;i++){
        var row = table.insertRow(i);
        var cell = row.insertCell(0);
        cell.innerHTML = "Some HTML here"
    };
}

AJAX请求是有效的,我已经确认了这一点,所以在var千seqResult中有一个结果加载(我已经在多个位置声明了这个变量,以确保它被加载)。然而,当dbload()完成时,structureTable()并没有被调用,我似乎不明白为什么。

任何帮助都将不胜感激。

一旦命中return语句,javascript将停止处理函数的其余部分,因此return之后的任何行都将被忽略。所以,切换这些的顺序,所以:

    structureTable();
    return kiloseqResult