简体   繁体   English

当我尝试暂停并在jsFiddle中播放时,我的代码可以工作,但在我的html页面中不起作用

[英]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>

This is optional: 这是可选的:

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.

相关问题 代码在jsfiddle中有效,但是当我将所有代码放入网站时不起作用 - Code works in jsfiddle but doesn't work when I put all the code into my website 代码在jsfiddle中有效,但在我的网站上不起作用 - Code works in jsfiddle but doesn't work on my site 上传代码后,我的代码无法在CodePen或JsFiddle上运行,但在浏览器中可以正常运行。 有人能帮我吗? - My code doesn't work on CodePen or JsFiddle when I upload it but works fine in my browser. Would anyone be able to help me please? 我尝试创建导航栏,但我的 javascript 代码不起作用 - i try to create a navbar but my javascript code doesn't work 我有这个代码在JSFiddle中工作,但不在我的网站上 - I have this code that works in JSFiddle but not on my site 我的 javascript 代码不起作用,但在控制台中执行时它起作用 - My javascript code doesn't work, but when it is executed in the console it works 加载html页面时,为什么我的外部javascript文件不起作用? - Why doesn't my external javascript file work when I load my html page? 我的网站不喜欢一行代码,但是可以在JSfiddle上运行 - My website doesn't like one line of my code but it works on JSfiddle 为什么我的代码只能在html中工作,而在将其放入Javascript页面时却不能工作? - Why does my code only works in html but does not work when I put it into a Javascript page? 当我尝试播放音乐 Discord Bot 时,它不播放音乐 - When I try to play my Music Discord Bot it doesn't play music
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM