2014-10-30 117 views
0

我正在寫一個基本上將數據庫加載爲表的腳本。這只是它的準系統版本,但應該涵蓋我想要做的一切。爲什麼我的函數沒有被調用?

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 kiloseqResult結果加載(和我已經聲明在多個位置的變量,以確保它被加載)。但是,當dbload()完成時,不會調用structureTable(),我似乎無法弄清楚爲什麼。

任何幫助將不勝感激。

+3

因爲你在'return'後面調用它? – 2014-10-30 19:59:58

+0

與你的問題沒有直接關係,但你的'

'元素(你不應該使用它)不是很接近。 – 2014-10-30 20:04:25

+0

我沒有意識到我返回後無法打電話。謝謝。 另外,是的,

不應該在那裏。非常感謝:) – Gina 2014-10-30 20:07:29

回答

2

一旦return聲明被擊中,JavaScript將停止處理您的其他功能,因此您的return之後的任何行都將被忽略。因此,切換這些命令,所以:

structureTable(); 
    return kiloseqResult 
+0

我 - 我什至不知道這是一件事。謝謝。那麼這是一個令人尷尬的愚蠢問題。 – Gina 2014-10-30 20:07:00

+1

謝謝@ialarmedalien。我用更好的解釋更新了答案。 – 2014-10-30 20:51:00

相關問題