2012-04-08 74 views
1

我發現自己寫了很多像下面這樣的代碼,而且我仍然遇到很多死亡衝突的球。它使整個代碼寫入過程變得更加痛苦,感覺應該是這樣。有更好的方法嗎?爲Safari開發javascript - 防止死亡球

function drawArrowsInDocument (document, boxes, context) { 
    console.log ("drawArrowsInDocument"); 
    var body = document.body; 
    if (!body) { 
     console.log ("no body!"); 
     return; 
    } 
    var rectangles = rectanglesWithBoxes(boxes); 
    if (!rectangles) { 
     console.log ("no rectangles!"); 
     return; 
    } 
    var descendants = body.childNodes; 
    if (!descendants) { 
     console.log ("no descendants"); 
     return; 
    } 
    var length = descendants.length; 
    if (length>10000) { 
     console.log ("too many descendants"); 
     return; 
    } 
    // now I know my variables actually exist, and I can do something with them. 

下面是我看到它崩潰時的情況。在這一點上,我唯一能做的就是重新啓動Safari,然後放入更多的if語句來查找問題。

crash

+1

我發現鉻少不穩定,幾乎相同的發動機。您也可以隨時使用'throw'來停止腳本。死亡球在無限循環中最常見,所以重新考慮那些循環。 – David 2012-04-08 19:32:28

回答