【问题标题】:How can I convert a vanilla JS stop watch to a Angular one?如何将香草 JS 秒表转换为 Angular 秒表?
【发布时间】:2017-05-06 20:33:01
【问题描述】:

JS 新手,如果不允许,将删除。我有一些常规的 JS 代码,我一直在尝试将其转换为控制器。

对于给定的示例,最优化的方式是什么。我见过带有指令、计数器等的案例,但是我认为这对于我正在做的事情来说非常复杂。到目前为止,我已经设法将秒和十位绑定到屏幕上,但是我在更新 $interval 时遇到问题,我需要 .watch、.promises .then 吗? (抱歉新手问题,任何帮助都会很棒)

JS 脚本

   window.onload = function() {

     var seconds = 00;
     var tens = 00;
     var appendTens = document.getElementById("tens");
     var appendSeconds = document.getElementById("seconds");
     var buttonStart = document.getElementById('button-start');
     var buttonStop = document.getElementById('button-stop');
     var buttonReset = document.getElementById('button-reset');
     var Interval;


     buttonStart.onclick = function () {

       clearInterval(Interval);
       Interval = setInterval(startTimer, 10);
     };

     buttonStop.onclick = function () {
       clearInterval(Interval);
     };


     buttonReset.onclick = function () {
       clearInterval(Interval);
       tens = "00";
       seconds = "00";
       appendTens.innerHTML = tens;
       appendSeconds.innerHTML = seconds;
       console.log("clear()");
     };


     function startTimer() {
       tens++;

       if (tens < 9) {
         appendTens.innerHTML = "0" + tens;
       }

       if (tens > 9) {
         appendTens.innerHTML = tens;

       }

       if (tens > 99) {
         console.log("seconds");
         seconds++;
         appendSeconds.innerHTML = "0" + seconds;
         tens = 0;
         appendTens.innerHTML = "0" + 0;
       }

       if (seconds > 9) {
         appendSeconds.innerHTML = seconds;
       }

     }

   };

这就是我将 HTML 绑定到屏幕的方式

&lt;span id="seconds"&gt;{{ seconds+":"+tens }}&lt;/span&gt;

【问题讨论】:

  • 如果您注释掉所有代码,它 (a) 将不会运行,并且 (b) 在向其他人显示时不会突出显示语法。删除每行前面的//
  • fixed** CMD /(我假设 CTRL 用于 windows)通常是一些 IDE 上的快捷方式来解决这个问题

标签: javascript angularjs intervals angular-promise


【解决方案1】:

我用一个简单的$interval 循环来更新秒和分钟:

angular.module('plunker', []).controller('MainCtrl', function($interval) {
  var interval_;

  this.seconds = 0;
  this.minutes = 0;

  this.start = function() {
    interval_ = $interval(function() {
      this.seconds++;
      if(this.seconds > 59) {
        this.seconds = 0;
        this.minutes++;
      }
    }.bind(this), 1000)
  }

  this.stop = function() {
    $interval.cancel(interval_)
  }
})
.filter('adjustTime', function(){
  return function(input) {
    if(input < 10) {
      return '0' + input;
    }
    return input;
  }
});

我是委托的粉丝,所以我制作了你看到的过滤器来处理添加额外的 0(如果它小于 10);无需搞砸$interval 逻辑。

Plnkr

【讨论】:

  • 您知道&lt;&gt; 按钮将在此处而不是在 Plnkr 创建一个 sn-p?
  • 还有.filter('adjustTime', function(){ return function(input) { return String("0"+input).slice(-2); }
  • 我没有。谢谢@mplungjan
  • @mplungjan 你能描述一下 .filter 函数的作用吗? .filter('adjustTime', function(){ return function(input) { return String("0"+input).slice(-2); }
  • 填充数字:如果输入为 2 个字符 - 11 -> 011 -> 11,则取包含 3 个字符的字符串的最后 2 个字符; 3-> 03 -> 03
猜你喜欢
  • 2022-09-22
  • 1970-01-01
  • 2017-06-19
  • 1970-01-01
  • 1970-01-01
  • 2015-11-14
  • 2011-06-02
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多