如何删除脚本并创建单独的js文件

How to remove script and create separate js file

本文关键字:单独 创建 js 文件 脚本 何删除 删除      更新时间:2023-09-26

我目前正在研究计时器。目前我有它,以便脚本卡在 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>