[英]When I try pause and play in jsFiddle, my code works but it doesn't work in my html page
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?
当我在jsFiddle中使用它时,它可以工作,但是当我将相同的代码复制到html页面中时,它不起作用吗? Can anyone explain why did this happen?
谁能解释为什么会这样?
The code in my HTML : 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. jsFiddle自动插入
<script src="jquery.js"></script>
标记。
Here, you must do it yourself. 在这里,您必须自己做。
<script src="https://code.jquery.com/jquery-1.11.2.min.js"></script>
Pro Tip: Use a CDN ( C ontent D elivery N etwork). 临提示:使用CDN(C ontent d eliveryÑetwork)。 They have thoroughly optimized delivery, and will often:
他们已经充分优化了投放方式,并且通常会:
I) Send the content from the server physically closest to the end user. I)从物理上最接近最终用户的服务器发送内容。 This leads to faster load speeds.
这导致更快的加载速度。
II) Caching in the browser. II)在浏览器中缓存。 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).
这意味着,如果网站www.example.com已经使用了所需的脚本,则永远不会再次加载该脚本(直到清除缓存)。 The old copy is used, and this means no loading time for the script.
使用了旧副本,这意味着该脚本没有加载时间。
In conclusion : Use a CDN! 结论 :使用CDN! They're often free!
他们通常是免费的! Why not?
为什么不?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.