0

I'm trying to perform a basic calculation in JavaScript that takes a decimal away from a whole number and no matter what I try I keep getting Not a Number (NaN) errors in the console. My starting number is 1000 then for each cycle on a SetInterval function I want to minus 0.1.

this.StartTimer = function () {
        var width = 100;
        this.QuestionScore = 1000;
        var thisQuiz = this;
        var elem = document.getElementById("barStatus");   
        // Set Interval
        var timer = setInterval(frame, 30);
        function frame() {
            if (width <= 0) {
                // Move to next Question when timer is up
                clearInterval(timer);
                elem.style.width = 100 + '%';
                if (this.QuestionsGone == 9) {
                    thisQuiz.GetSummary(this.Type);
                } else {
                    thisQuiz.AnswerQuestion(-1, 0);
                }
            } else {
                // Increase Status Bar Each Interval
                width = width - 0.1; 
                this.QuestionScore = this.QuestionScore - 0.1;
                console.log(this.QuestionScore);
                this.QuestionScore = Math.ceil(this.QuestionScore * 10) / 10;
                console.log(this.QuestionScore);
                elem.style.width = width + '%'; 
            }
        }
    }
Web Develop Wolf
  • 5,996
  • 12
  • 52
  • 101
  • 2
    please add the whole code with mentioned `setInterval`. – Nina Scholz Feb 16 '17 at 09:40
  • 1
    `NaN` isn't an error and the code you've posted doesn't return `NaN`. Please post the code which is generating the issue you're having. – James Donnelly Feb 16 '17 at 09:40
  • Posted the full function – Web Develop Wolf Feb 16 '17 at 09:42
  • 2
    Could you provide a real [mcve]. Running your code http://jsbin.com/roxevec/1/edit?js,console doesn't demonstrate the problem you describe. – Quentin Feb 16 '17 at 09:43
  • 1
    `this` in the interval callback, whatever you're expecting it to refer, it isn't refererring that object. – Teemu Feb 16 '17 at 09:48
  • Put the code in a [JSBin](https://jsbin.com/bibonojode/2/edit?js,console) for anyone that wants to look at this. It's not returning NaN. – George Feb 16 '17 at 09:49
  • 1
    As Teemu alludes, replace the `this` references with `thisQuiz` within `frame()`. Because of the callback, `this` does not equal the `this` you are expecting it to be – K Scandrett Feb 16 '17 at 09:58

0 Answers0