7

I have a basic function in JavaScript that simply takes some pre-set values and puts them onto the screen by the use of a pre-made function. When I breakpoint the first line of what it is I'm doing, the page loads fine, as expected, but as soon as I remove that breakpoint, none of the information is set. and the page is blank.

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);

The functions inside that (SetBackground and LoadInnerHTML) are just small one line functions that change the inner html and the set a background image.

// 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)';
}

I can't understand why it wouldn't work when the breakpoint isn't on. Clearly it does work, because everything is fine with the breakpoint on, but then when it's off the result I get output to the console is:

1     
2 
3 undefined 
Quiz Selection 
Web Develop Wolf
  • 5,996
  • 12
  • 52
  • 101
  • 1
    It's not how you show the data, it's how you get it in the first place. Where are `this.Title`, `this.Introduction` etc coming from? – JJJ Feb 13 '17 at 10:01
  • Coming from a JSON file - they load in fine when I watch the variables in the debugger – Web Develop Wolf Feb 13 '17 at 10:03
  • 1
    Ajax call? Best guess: [How do I return the response from an asynchronous call?](http://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call) – JJJ Feb 13 '17 at 10:04
  • It sounds like you're not running this code in the AJAX callback. When you stop at the breakpoint, it allows the callback function to run first and set the variable. Without the breakpoint, this code runs before the AJAX finishes. – Barmar Feb 13 '17 at 10:05

2 Answers2

4

You have a race condition.

The act of hitting a breakpoint makes your code wait for the async JSON load to complete. Without the breakpoint, the code trying to read the JSON is executing before the JSON has actually loaded.

See How do I return the response from an asynchronous call? for how to fix this issue.

Community
  • 1
  • 1
Jamiec
  • 133,658
  • 13
  • 134
  • 193
  • Just had a look at that - biggest problem is that I can't use JQuery - I can only use JS - can I do the sme thing inm that post above without JQuery? – Web Develop Wolf Feb 13 '17 at 10:06
  • 1
    @WebDevelopWolf yes, there is nothing specific about jQuery which makes that possible. Your json load will have a way to execute code when the load is complete – Jamiec Feb 13 '17 at 10:07
-4

You have console.log statements in your code. When the debugger is not on, console object does not exist (this is true for IE not for Chrome to the best of my knowledge), thus your javascript code execution fails.

dubes
  • 5,324
  • 3
  • 34
  • 47
  • 1
    The question says "when it's off the result I get output to the console" so it can't be due to exceptions thrown by a missing console object. – Quentin Feb 13 '17 at 10:02
  • I'm not using IE I'm using Firefox at the moment - but what @Quentin said was true too that I only get those errors when the breakpoint ISN'T set - thanks for the info though as I didn't know that :) – Web Develop Wolf Feb 13 '17 at 10:24