I found a code online to pause and play a timer. When I used it in jsFiddle, it worked but when I copied the same code into my html page, it didn't work? Can anyone explain why did this happen?
The code in my HTML :
<body onload="start();">
<script>
function start() {
var output = $('h1');
var isPaused = false;
var time = 30;
var t = window.setInterval(function() {
if(!isPaused) {
time--;
output.text("0:" +time);
}
}, 1000);
}
//with jquery
$('.pause').on('click', function(e) {
e.preventDefault();
isPaused = true;
});
$('.play').on('click', function(e) {
e.preventDefault();
isPaused = false;
});
</script>
<a href="#" class="play">Play</a>
<a href="#" class="pause">Pause</a>
<h1>0</h1>
</body>
jsFiddle inserts the <script src="jquery.js"></script>
tag automatically.
Here, you must do it yourself.
<script src="https://code.jquery.com/jquery-1.11.2.min.js"></script>
Pro Tip: Use a CDN (Content Delivery Network). They have thoroughly optimized delivery, and will often:
I) Send the content from the server physically closest to the end user. This leads to faster load speeds.
II) Caching in the browser. This means that if the site www.example.com already uses the script you want, it never gets loaded again (until the cache is cleared). The old copy is used, and this means no loading time for the script.
In conclusion: Use a CDN! They're often free! Why not?
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments