1

I was working on a Timer code and was facing issue with it and posted a question regarding it. After discussion, thanks to @James, I found it was JSFiddle issue.

JSFiddle Link: http://jsfiddle.net/RajeshDixit/0gvfc5yy/4/

CodePen Link: http://codepen.io/rajesh_dixit/pen/RWBowQ

When you run it on JSFiddle, you will get following error

Uncaught ReferenceError: startTimer is not defined

But works fine on CodePen and in snippet below.

function timer() {
    var time = {
        sec: 00,
        min: 00,
        hr: 00
    }
    var max = 59;
    var interval = null;

    function update(str) {
        time[str]++;
        time[str] = time[str] % 60;
        if (time[str] == 0) {
            str == "sec" ? update("min") : update("hr");
        }
        print(str);
    }

    function print(str) {
        var _time = time[str].toString().length == 1 ? "0" + time[str] : time[str];
        document.getElementById("lbl" + str).innerHTML = _time;
    }

    function initInterval() {
        interval = setInterval(function () {
            update("sec");
        }, 1000);
    }

    function stopTimer() {
        clearInterval(interval);
    }

    return {
        'init': initInterval,
            'stop': stopTimer
    }
};

var time = new timer();

function startTimer() {
    time.init();
}

function endTimer() {
    time.stop();
}
<div> <span id="lblhr">00</span>
: <span id="lblmin">00</span>
: <span id="lblsec">00</span>

</div>
<button onclick="startTimer()">Start</button>
<button onclick="endTimer()">Stop</button>

1 Answer 1

1

In jsFiddle, the JS loading option can be configured. Change it from onload to in <head> tag. It works, check this updated one.

Check here JSFiddle documentation for more information.

Sign up to request clarification or add additional context in comments.

1 Comment

Yup that was the issue. Thanks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.