2017-02-13 60 views
0

我在JavaScript中有一個基本功能,只需使用一些預設值並通過使用預製功能將它們放到屏幕上。當我斷開第一行時,頁面加載正常,但是一旦我移除該斷點,就沒有設置任何信息。並且頁面是空白的。JavaScript僅適用於在瀏覽器調試器中設置的斷點

this.QuizSelection = function() { 
     // Fill the ID's with the right info 
     app.SetBackground('head', this.HeroImage); 
     console.log('1 ' + this.HeroImage); 
     app.LoadInnerHTML('breadcrumbs', 'Home/' + this.Title); 
     app.LoadInnerHTML('quizSelectionTitle',this.Title); 
     console.log('2 ' + this.Title); 
     app.LoadInnerHTML('quizSelectionIntro',this.Introduction); 
     console.log('3 ' + this.Introduction); 
     // Show the Quiz Selection and Heading 
     app.ShowSection('head'); 
     app.ShowSection('quizSelection'); 
     console.log('Quiz Selection'); 
    }.bind(this); 

即(的setBackground和LoadInnerHTML)內的函數是改變內HTML和設置背景圖像只是小一個行功能。

// Change Inner HTML 
this.LoadInnerHTML = function (id, html) { 
    var d = document.getElementById(id); 
    d.innerHTML = html; 
} 

// Set Background Image 
this.SetBackground = function (id, image) { 
    document.getElementById(id).style.backgroundImage = 'url(image)'; 
} 

我不明白爲什麼它不會工作,當斷點不開。顯然,它的工作,因爲一切都很好用的斷點,但是當它關​​閉的結果我得到輸出到控制檯:

1  
2 
3 undefined 
Quiz Selection 
+1

這不是你如何顯示數據,這是你如何得到它的第一個地方。 「this.Title」,「this.Introduction」等來自哪裏? – JJJ

+0

來自JSON文件 - 當我在調試器 –

+1

Ajax調用中觀察變量時,它們加載得很好嗎?最佳猜測:[如何返回來自異步調用的響應?](http://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call) – JJJ

回答

2

你有一個競賽條件。

打斷點的行爲使您的代碼等待異步JSON加載完成。沒有斷點,試圖讀取JSON的代碼在JSON實際加載之前執行。

請參閱How do I return the response from an asynchronous call?瞭解如何解決此問題。

+0

剛看了一下 - 最大的問題是,我不能使用JQuery - 我只能使用JS - 我可以做的東西上面沒有JQuery sme的東西嗎? –

+1

@WebDevelopWolf是的,沒有什麼具體的關於jQuery的,這使得可能。加載完成後,您的json加載將有一種方法來執行代碼 – Jamiec

-4

你在你的代碼console.log語句。當調試器沒有打開時,控制檯對象不存在(在我所知的情況下,IE不適用於Chrome),因此您的JavaScript代碼執行失敗。

+1

這個問題說:「什麼時候結果我得到輸出到控制檯」,所以它不能由於缺少控制檯對象拋出異常。 – Quentin

+0

我沒有使用IE我現在使用的是Firefox - 但是@Quentin所說的是真的,我只在斷點沒有設置時纔會出現這些錯誤 - 感謝信息,雖然我不知道:) –

相關問題