2017-03-31 3 views
0

ちょっと前にイベントのためにした友達のタイマーを更新しようとしています。タイマーは完全に機能し、既に必要なものすべてを実行します(スニペットを表示して実行する)。しかし、ここでは秒の代わりにmm:ssの形式を表示したいと考えています(例:180 - > 3:00)。もちろん、これはさらにカウントダウンすることができなければなりません。私がグーグル・グーグルでやったアプローチは失敗した私の問題をどのようにエレガントに解決するか考えている人はいますか?秒単位のミリ秒単位の変換:JavaScriptの実行中のタイマー

編集:あなたがここにスペース "

<!DOCTYPE html> 
 
<html> 
 

 
<head> 
 
    <title></title> 
 

 
    <style type="text/css"> 
 
    body { 
 
     background-color: #333; 
 
     font-family: sans-serif; 
 
    } 
 
    
 
    .item { 
 
     position: absolute; 
 
     top: 50%; 
 
     left: 50%; 
 
     transform: translateX(-50%) translateY(-50%) scale(3.0); 
 
    } 
 
    
 
    .item h1 { 
 
     text-align: center; 
 
     position: absolute; 
 
     width: 100%; 
 
     top: 50%; 
 
     left: 50%; 
 
     transform: translateX(-50%) translateY(-110%); 
 
     color: #ffffff; 
 
     font-size: 3em; 
 
    } 
 
    
 
    .item h2 { 
 
     text-align: center; 
 
     position: absolute; 
 
     width: 50%; 
 
     top: 50%; 
 
     left: 50%; 
 
     transform: translateX(-50%) translateY(-110%); 
 
     color: #ffffff; 
 
     font-size: 1.5em; 
 
     line-height: 0.9; 
 
    } 
 
    
 
    svg { 
 
     -webkit-transform: rotate(-90deg); 
 
     transform: rotate(-90deg); 
 
    } 
 
    
 
    .circle_animation { 
 
     stroke-dasharray: 440; 
 
     /* this value is the pixel circumference of the circle */ 
 
     stroke-dashoffset: 440; 
 
     transition: all 1s linear; 
 
    } 
 
    </style> 
 

 
    <script src="https://code.jquery.com/jquery-2.2.1.min.js"></script> 
 

 
    <script type="text/javascript"> 
 
    var running = false; 
 

 
    var time = 180; 
 
    var initialOffset = '440'; 
 
    var runtime = 0; 
 

 
    $(document).keydown(function(e) { 
 

 
     switch (e.which) { 
 
     case 32: 
 
      setTimeout(function() { 
 
      if (running === false) { 
 
       running = true; 
 
      } else { 
 
       running = false; 
 
      }; 
 
      }, 1); 
 
      break; 
 
     } 
 
    }); 
 

 
    $(document).ready(function() { 
 

 
     console.log("ready!"); 
 
     $('#start').click(function() { 
 
     running = true; 
 
     }); 
 

 
     var interval = setInterval(function() { 
 
     $('.circle_animation').css('stroke-dashoffset', initialOffset - (runtime * (initialOffset/(time + 10.5)))); 
 
     $('h1').text(time - runtime); 
 
     if (runtime == 420) { 
 
      audioElement1.play(); 
 
     } 
 
     if (runtime == time) { 
 
      clearInterval(interval); 
 
      $('.circle_animation').css('stroke-dashoffset', 880); 
 
      $('h1').text(''); 
 
      $('h2').text('Time is up!'); 
 
     } 
 
     if (running) { 
 
      runtime++; 
 
     }; 
 
     }, 1000); 
 

 
    }); 
 
    </script> 
 

 
</head> 
 

 
<body> 
 

 
    <div class="item html"> 
 
    <svg width="160" height="160" xmlns="http://www.w3.org/2000/svg"> 
 
     <g> 
 
     <title>Layer 1</title> 
 
     <circle id="circle" r="65.85699" cy="81" cx="81" stroke-width="15" stroke="#ffffff" fill="none"/> 
 
     <circle id="circle" class="circle_animation" r="65.85699" cy="81" cx="81" stroke-width="16" stroke="#333" fill="none"/> 
 
     </g> 
 
    </svg> 
 
    <h1>180</h1> 
 
    <h2></h2> 
 
    </div> 
 

 
</body> 
 

 
</html>

答えて

1

利用モジュラス数学:

function sec2human(seconds) { 
    sec = seconds % 60; 
    min = parseInt(seconds/60); 
    if(sec.toString().length == 1) { // padding 
     sec = "0" + sec; 
    } 
    return min + ":" + sec; 
} 

全作業例:

<!DOCTYPE html> 
 
<html> 
 

 
<head> 
 
    <title></title> 
 

 
    <style type="text/css"> 
 
    body { 
 
     background-color: #333; 
 
     font-family: sans-serif; 
 
    } 
 

 
    .item { 
 
     position: absolute; 
 
     top: 50%; 
 
     left: 50%; 
 
     transform: translateX(-50%) translateY(-50%) scale(3.0); 
 
    } 
 

 
    .item h1 { 
 
     text-align: center; 
 
     position: absolute; 
 
     width: 100%; 
 
     top: 50%; 
 
     left: 50%; 
 
     transform: translateX(-50%) translateY(-110%); 
 
     color: #ffffff; 
 
     font-size: 3em; 
 
    } 
 

 
    .item h2 { 
 
     text-align: center; 
 
     position: absolute; 
 
     width: 50%; 
 
     top: 50%; 
 
     left: 50%; 
 
     transform: translateX(-50%) translateY(-110%); 
 
     color: #ffffff; 
 
     font-size: 1.5em; 
 
     line-height: 0.9; 
 
    } 
 

 
    svg { 
 
     -webkit-transform: rotate(-90deg); 
 
     transform: rotate(-90deg); 
 
    } 
 

 
    .circle_animation { 
 
     stroke-dasharray: 440; 
 
     /* this value is the pixel circumference of the circle */ 
 
     stroke-dashoffset: 440; 
 
     transition: all 1s linear; 
 
    } 
 
    </style> 
 

 
    <script src="https://code.jquery.com/jquery-2.2.1.min.js"></script> 
 

 
    <script type="text/javascript"> 
 
    var running = false; 
 

 
    var time = 180; 
 
    var initialOffset = '440'; 
 
    var runtime = 0; 
 

 
    $(document).keydown(function(e) { 
 

 
     switch (e.which) { 
 
     case 32: 
 
      setTimeout(function() { 
 
      if (running === false) { 
 
       running = true; 
 
      } else { 
 
       running = false; 
 
      }; 
 
      }, 1); 
 
      break; 
 
     } 
 
    }); 
 

 
    $(document).ready(function() { 
 

 
     console.log("ready!"); 
 
     $('#start').click(function() { 
 
     running = true; 
 
     }); 
 

 
     $('h1').text(sec2human(time)); // added for initial display 
 

 
     var interval = setInterval(function() { 
 
     $('.circle_animation').css('stroke-dashoffset', initialOffset - (runtime * (initialOffset/(time + 10.5)))); 
 
     $('h1').text(sec2human(time - runtime)); // added function call 
 
     if (runtime == 420) { 
 
      audioElement1.play(); 
 
     } 
 
     if (runtime == time) { 
 
      clearInterval(interval); 
 
      $('.circle_animation').css('stroke-dashoffset', 880); 
 
      $('h1').text(''); 
 
      $('h2').text('Time is up!'); 
 
     } 
 
     if (running) { 
 
      runtime++; 
 
     }; 
 
     }, 1000); 
 

 
    }); 
 

 
    function sec2human(seconds) { 
 
     sec = seconds % 60; 
 
     min = parseInt(seconds/60); 
 
     if(sec.toString().length == 1) { // padding 
 
      sec = "0" + sec; 
 
     } 
 
     return min + ":" + sec; 
 
    } 
 
    </script> 
 

 
</head> 
 

 
<body> 
 

 
    <div class="item html"> 
 
    <svg width="160" height="160" xmlns="http://www.w3.org/2000/svg"> 
 
     <g> 
 
     <title>Layer 1</title> 
 
     <circle id="circle" r="65.85699" cy="81" cx="81" stroke-width="15" stroke="#ffffff" fill="none"/> 
 
     <circle id="circle" class="circle_animation" r="65.85699" cy="81" cx="81" stroke-width="16" stroke="#333" fill="none"/> 
 
     </g> 
 
    </svg> 
 
    <h1>180</h1> 
 
    <h2></h2> 
 
    </div> 
 

 
</body> 
 

 
</html>

1

を押して、タイマーを開始し、停止することができますが、重要な部分です:

var seconds = time - runtime, 
     mins = ("0" + Math.floor(seconds/60)).substr(-2), 
     secs = ("0" + (seconds % 60)).substr(-2); 
    $('h1').text(mins + ":" + secs); 

基本的にして、各パートを分割します分、秒を入力して連結します。ストリングは二桁パディング強制することである(すなわち:2時03の代わりに2:3)ところで

<!DOCTYPE html> 
 
<html> 
 

 
<head> 
 
    <title></title> 
 

 
    <style type="text/css"> 
 
    body { 
 
     background-color: #333; 
 
     font-family: sans-serif; 
 
    } 
 
    
 
    .item { 
 
     position: absolute; 
 
     top: 50%; 
 
     left: 50%; 
 
     transform: translateX(-50%) translateY(-50%) scale(3.0); 
 
    } 
 
    
 
    .item h1 { 
 
     text-align: center; 
 
     position: absolute; 
 
     width: 100%; 
 
     top: 50%; 
 
     left: 50%; 
 
     transform: translateX(-50%) translateY(-110%); 
 
     color: #ffffff; 
 
     font-size: 3em; 
 
    } 
 
    
 
    .item h2 { 
 
     text-align: center; 
 
     position: absolute; 
 
     width: 50%; 
 
     top: 50%; 
 
     left: 50%; 
 
     transform: translateX(-50%) translateY(-110%); 
 
     color: #ffffff; 
 
     font-size: 1.5em; 
 
     line-height: 0.9; 
 
    } 
 
    
 
    svg { 
 
     -webkit-transform: rotate(-90deg); 
 
     transform: rotate(-90deg); 
 
    } 
 
    
 
    .circle_animation { 
 
     stroke-dasharray: 440; 
 
     /* this value is the pixel circumference of the circle */ 
 
     stroke-dashoffset: 440; 
 
     transition: all 1s linear; 
 
    } 
 
    </style> 
 

 
    <script src="https://code.jquery.com/jquery-2.2.1.min.js"></script> 
 

 
    <script type="text/javascript"> 
 
    var running = false; 
 

 
    var time = 180; 
 
    var initialOffset = '440'; 
 
    var runtime = 0; 
 

 
    $(document).keydown(function(e) { 
 

 
     switch (e.which) { 
 
     case 32: 
 
      setTimeout(function() { 
 
      if (running === false) { 
 
       running = true; 
 
      } else { 
 
       running = false; 
 
      }; 
 
      }, 1); 
 
      break; 
 
     } 
 
    }); 
 

 
    $(document).ready(function() { 
 

 
     console.log("ready!"); 
 
     $('#start').click(function() { 
 
     running = true; 
 
     }); 
 

 
     var interval = setInterval(function() { 
 
     $('.circle_animation').css('stroke-dashoffset', initialOffset - (runtime * (initialOffset/(time + 10.5)))); 
 
     var seconds = time - runtime, 
 
      mins = ("0" + Math.floor(seconds/60)).substr(-2), 
 
      secs = ("0" + (seconds % 60)).substr(-2); 
 
     $('h1').text(mins + ":" + secs); 
 
     if (runtime == 420) { 
 
      audioElement1.play(); 
 
     } 
 
     if (runtime == time) { 
 
      clearInterval(interval); 
 
      $('.circle_animation').css('stroke-dashoffset', 880); 
 
      $('h1').text(''); 
 
      $('h2').text('Time is up!'); 
 
     } 
 
     if (running) { 
 
      runtime++; 
 
     }; 
 
     }, 1000); 
 

 
    }); 
 
    </script> 
 

 
</head> 
 

 
<body> 
 

 
    <div class="item html"> 
 
    <svg width="160" height="160" xmlns="http://www.w3.org/2000/svg"> 
 
     <g> 
 
     <title>Layer 1</title> 
 
     <circle id="circle" r="65.85699" cy="81" cx="81" stroke-width="15" stroke="#ffffff" fill="none"/> 
 
     <circle id="circle" class="circle_animation" r="65.85699" cy="81" cx="81" stroke-width="16" stroke="#333" fill="none"/> 
 
     </g> 
 
    </svg> 
 
    <h1>180</h1> 
 
    <h2></h2> 
 
    </div> 
 

 
</body> 
 

 
</html>

クールなアニメーション。

関連する問題