JavaScript 时钟不工作

标签 javascript html css raphael

我正在使用 Raphael 为我的网站制作一个简单的时钟。我正在尝试绘制秒针以使其像普通时钟一样滴答作响。

这是我正在运行的代码:

window.onload = function() {

  var paper = new Raphael(0, 0, 400, 400);

  var backGround = paper.rect(0, 0, 400, 400);

  backGround.attr({
    fill: "orange"
  });

  var face = paper.circle(100, 100, 95);
  face.attr({
    "fill": "#f5f5f5",
    "stroke": "#ff0000",
    "stroke-width": "3"
  })

  var hand = paper.path("M100 110L100 25");
  hand.attr({
    stroke: "#ffff00",
    "stroke-width": 1
  });

  function startTime() {
    var hands = now.getSeconds();
    hands = checkTime(hands);
    hand.animate({
      transform: ['r', ((hands * 6) - 180), 200, 200]
    });

    setTimeout(function() {
      startTime()
    }, 1000);

    function checkTime(i) {
      if (i < 10) {
        i = "0" + i
      };
      return i;

    }
    startTime();

  }
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/raphael/2.1.4/raphael-min.js"></script>

最佳答案

  • 您需要在函数本身之外启动时间函数
  • 您错过了新的日期声明
  • 我也将手移动到 100x100
  • 删除了无用的第二个格式化程序。
  • 将手变成黑色以便看得更清楚

window.onload = function() {

  var paper = new Raphael(0, 0, 400, 400);

  var backGround = paper.rect(0, 0, 400, 400);

  backGround.attr({
    fill: "orange"
  });

  var face = paper.circle(100, 100, 95);
  face.attr({
    "fill": "#f5f5f5",
    "stroke": "#ff0000",
    "stroke-width": "3"
  })

  var hand = paper.path("M100 110L100 25");
  hand.attr({
    stroke: "#000000",
    "stroke-width": 1
  });

  function startTime() {
    var now = new Date(),
        hands = now.getSeconds();
    hand.animate({
      transform: ['r', ((hands * 6) - 180), 100, 100]
    });

    setTimeout(function() {
      startTime()
    }, 1000);
  }
  startTime();

}
<script src="https://cdnjs.cloudflare.com/ajax/libs/raphael/2.1.4/raphael-min.js"></script>

关于JavaScript 时钟不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35312225/

相关文章:

javascript - 我怎样才能只选择表格的一列?

javascript - 有没有JS触屏 Controller 库?

jquery - 在访问子菜单时保持父 li 突出显示

html - 如何在 Angular 输入中添加垫选择

javascript - 从 Bing Maps API 中的 URL 添加 PolygonOptions 到 GeoJSON

javascript - 仅使用 javascript 清除 DIV 中的所有文本字段

html - 两次放置相同的链接

html - 在右下角设置背景图片,但图片底部被截掉

javascript - 从 Chrome 应用程序在浏览器中打开标签页

html - 根据第二个 float 的 div 内容调整 float 的第一个 div 的大小?