如何删除脚本并创建单独的js文件
How to remove script and create separate js file
我目前正在研究计时器。目前我有它,以便脚本卡在 html 中以运行它。我想将 js 分成一个单独的文件。这是我到目前为止所拥有的。
.HTML
<body>
<div id="countdown"></div>
<div id="notifier"></div>
<script type="text/javascript">
function startTimer() {
userInput = document.getElementById('userTime').value;
if (userInput.length == 0) {
alert("Please enter a value");
} else {
var numericExpression = /^[0-9]+$/;
if (!userInput.match(numericExpression)) {
alert("Please enter a number")
} else {
function display(notifier, str) {
document.getElementById(notifier).innerHTML = str;
}
function toMinuteAndSecond(x) {
return Math.floor(x / 60) + ":" + x % 60;
}
function setTimer(remain, actions) {
(function countdown() {
display("countdown", toMinuteAndSecond(remain));
actions[remain] && actions[remain]();
(remain -= 1) >= 0 && setTimeout(arguments.callee, 1000);
})();
}
setTimer(userInput, {
10: function() {
display("notifier", "Just 10 seconds to go");
},
5: function() {
display("notifier", "5 seconds left");
},
0: function() {
alert("Time is up. Please Sumbit Vote.");
}
});
}
}
}
</script>Set Time Limit:
<input type="text" id="userTime" />
<input type="button" value="Start" onclick="startTimer()" />
</body>
这是一个小提琴http://jsfiddle.net/grahamwalsh/0h9t65bs/
使用名为 file.js
的 javascript 代码制作一个文件 然后在脚本标记中执行以下操作,
<script type="text/javascript" src="file.js" ></script>
这将使 javascript 文件被加载。
创建一个名为 script.js
或任何您喜欢的名称的新文件。确保它与 html 文件位于同一目录中。
接下来,将 html 脚本复制到脚本中.js并将<script type="text/javascript">
更改为<script type="text/javascript" src="script.js">
编辑:结果代码:
function startTimer() {
userInput = document.getElementById('userTime').value;
if(userInput.length == 0) {
alert("Please enter a value");
} else {
var numericExpression = /^[0-9]+$/;
if(!userInput.match(numericExpression)) {
alert("Please enter a number")
} else {
function display( notifier, str ) {
document.getElementById(notifier).innerHTML = str;
}
function toMinuteAndSecond( x ) {
return Math.floor(x/60) + ":" + x%60;
}
function setTimer( remain, actions ) {
(function countdown() {
display("countdown", toMinuteAndSecond(remain));
actions[remain] && actions[remain]();
(remain -= 1) >= 0 && setTimeout(arguments.callee, 1000);
})();
}
setTimer(userInput, {
10: function () { display("notifier", "Just 10 seconds to go"); },
5: function () { display("notifier", "5 seconds left"); },
0: function () { alert( "Time is up. Please Submit Vote."); }
});
}
}
}
<body>
<div id="countdown"></div>
<div id="notifier"></div>
<script type="text/javascript" src="script.js"></script>
Set Time Limit: <input type="text" id="userTime" />
<input type="button" value="Start" onclick="startTimer()" />
</body>
相关文章:
- Codrops页面转换.如何创建单独的按钮,而不是列表项目的效果
- 使用php创建的下拉列表中的选择单独填充文本区域
- 创建新数据和加载现有数据需要单独的视图吗
- d3.js如何为国家地图中的每个州创建一个单独的元素
- 画布创建两个单独的动画瀑布对象
- 从两个单独的函数创建 JQuery 悬停
- 创建单独的组件角度 2.
- 如何在创建动态输入文件时获取相同输入类型的单独 ID
- 从单独的 html 页面创建一个 dom 元素
- 创建单独的 js 文件进行包含
- 我是否必须为每个 webgl 程序创建单独的缓冲区
- 如何删除脚本并创建单独的js文件
- 从单独下载的 JPEG 创建影片时帧速率太慢
- 在两个单独的页面之间创建上滑过渡
- 如何使用骨干.js创建单独的模型、视图和控制器
- 如何为 cordova 应用程序创建一个单独的目录并在其中存储数据
- 如何使用 JSON 创建链接,单击这些链接时会显示在单独的
- 单独 HTML 文件的占位符表示法,用于创建网页的一小部分并将占位符替换为数据值
- 为每个选项卡单独创建 cookie
- 未捕获的ReferenceError: {var}未定义.文件是单独创建的