简体   繁体   中英

JS Code Works in jsbin and Not in jsfiddle or Chrome/Safari?

I am trying to figure out why this code is working only in jsbin and not in jsfiddle or in any web browser as an html/js file. I have tried debugging but cannot find a conclusion.

I made the mistake of coding directly in jsbin instead of a document. Any input would be appreciated.

http://jsbin.com/tuduxedohe/7/edit
http://jsfiddle.net/2rs1x5pz/

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>Timer</title>
  <script type="text/javascript" src="part2.js"></script>
</head>
<body>

<h1><div id="time">00:00:00</div></h1>
<div id="result"></div>
<button id="start" onclick ="startClock();" >Start</button>
<button id="stop" onclick="stopTimer();">Stop</button>
<button id="clear" onclick="resetTimer();">Reset</button>

</body>
</html>

var currentTime = document.getElementById('time');

var hundreths = 0;
var seconds = 0;
var minutes = 0;
var t;

function startClock() {
function add() {



    hundreths++;
    if (hundreths > 99) {
        hundreths = 0;
        seconds++;
        if (seconds > 59) {
            seconds = 0;
            minutes++;
        }
        if(minutes >= 10) {
            seconds= 0;
            minutes= 0;
            stopTimer();

        }
    }



if (hundreths > 9 && seconds < 9) {
currentTime.innerHTML = "0" + minutes + ":" + "0" + seconds + ":" + hundreths;
  }
else if ((seconds > 9 ) && (hundreths < 9)) {
currentTime.innerHTML = "0" + minutes + ":" + seconds + ":" + "0" + hundreths;
}
else if((seconds > 9) && (hundreths > 9)) {
currentTime.innerHTML = "0" + minutes + ":" + seconds + ":" + hundreths;
}
else if ((minutes > 9) && (seconds < 9) && (hundreths < 9)) {
currentTime.innerHTML = minutes + ":" + "0" + seconds + ":" + "0" + hundreths;
}

else if ((minutes > 9) && (seconds > 9) && (hundreths < 9)) {
currentTime.innerHTML = minutes + ":" + seconds + ":" + "0" + hundreths;
}

else if ((minutes > 9) && (seconds > 9) && (hundreths < 9)) {
currentTime.innerHTML = minutes + ":" + seconds + ":" + hundreths;
}

else {
currentTime.innerHTML = "0" + minutes + ":" + "0" + seconds + ":" + "0" + hundreths;
}

   timer();
}


function timer() {
    t = setTimeout(add, 1);
}
timer();
} // end function start clock

function stopTimer() {
    document.getElementById("result").innerHTML = "<p>" + ("Your time is: " + minutes + " minutes, " + seconds + " seconds, " + "and " + hundreths + " hundreths") + "</p>";
    clearTimeout(t);
}

function resetTimer() {
  hundreths = 0; 
  seconds = 0; 
  minutes = 0;   

  currentTime.innerHTML = "00:00:00";
}

That is because by default the script is added in a onload handler in jsfiddle, so your methods is available only inside the scope of that closure. So it will be

window.onload=function(){
    //your script is here
}

You are trying to access them in global scope when you are trying to call them from on<event> ="" attributes which will give an error like Uncaught ReferenceError: startClock is not defined in your console.

Change the second dropdown in the left panel under Frameworks & Extensions to body/head in the left panel of fiddle to add the script without a wrapper function

Demo: Fiddle

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM