脚本外部链接到js文件

时间:2015-03-23 17:58:41

标签: javascript

有人可以帮助我将外部js脚本从html页面“转换”为.js文件中的脚本吗? 我有一个倒计时脚本,其中一部分是这些外部链接,我不喜欢它,我可以把他变成一个js fie ??

 <script type="text/javascript">
  $(function() {
    var endDate = "June 7, 2087 15:03:25";

    $('.countdown.simple').countdown({ date: endDate });

    $('.countdown.styled').countdown({
      date: endDate,
      render: function(data) {
        $(this.el).html("<div>" + this.leadingZeros(data.years, 4) + " <span>years</span></div><div>" + this.leadingZeros(data.days, 3) + " <span>days</span></div><div>" + this.leadingZeros(data.hours, 2) + " <span>hrs</span></div><div>" + this.leadingZeros(data.min, 2) + " <span>min</span></div><div>" + this.leadingZeros(data.sec, 2) + " <span>sec</span></div>");
      }
    });

    $('.countdown.callback').countdown({
      date: +(new Date) + 10000,
      render: function(data) {
        $(this.el).text(this.leadingZeros(data.sec, 2) + " sec");
      },
      onEnd: function() {
        $(this.el).addClass('ended');
      }
    }).on("click", function() {
      $(this).removeClass('ended').data('countdown').update(+(new Date) + 10000).start();
    });

    // End time for diff purposes
    var endTimeDiff = new Date().getTime() + 15000;
    // This is server's time
    var timeThere = new Date();
    // This is client's time (delayed)
    var timeHere = new Date(timeThere.getTime() - 5434);
    // Get the difference between client time and server time
    var diff_ms = timeHere.getTime() - timeThere.getTime();
    // Get the rounded difference in seconds
    var diff_s = diff_ms / 1000 | 0;

    var notice = [];
    notice.push('Server time: ' + timeThere.toDateString() + ' ' + timeThere.toTimeString());
    notice.push('Your time: ' + timeHere.toDateString() + ' ' + timeHere.toTimeString());
    notice.push('Time difference: ' + diff_s + ' seconds (' + diff_ms + ' milliseconds to be precise). Your time is a bit behind.');

    $('.offset-notice').html(notice.join('<br />'));

    $('.offset-server .countdown').countdown({
      date: endTimeDiff,
      offset: diff_s * 1000,
      onEnd: function() {
        $(this.el).addClass('ended');
      }
    });

    $('.offset-client .countdown').countdown({
      date: endTimeDiff,
      onEnd: function() {
        $(this.el).addClass('ended');
      }
    });

  });
</script>

有代码。 提前致谢:D

1 个答案:

答案 0 :(得分:1)

是的,你可以。创建一个新文件并将其命名为countdown.js并将其放在与html文件相同的文件夹中。

然后从你的html页面内添加

<script src="countdown.js"></script>