2016-08-05 18 views
-1

毎日9時から5時30分に稼いだお金の量を増やしたいと思っています。特定の時間帯の数字を増やすjavascript

本質的に、夜の5時30分の場合、「お金の稼ぎ」は翌日の9:30まで停止し、5:30まで毎秒増分します。

これまでの説明はここにあります。

//Different toolkits for different browsers. If none work, set to null. 
 
var animFrame = window.requestAnimationFrame || window.webkitRequestAnimationFrame || window.mozRequestAnimationFrame || window.oRequestAnimationFrame || window.msRequestAnimationFrame || null; 
 

 
//Creating instances of canvas and the canvas' 2d drawing, allowing us to manipulate it in JS 
 
var c = document.getElementById("canvas"); 
 
var ctx = c.getContext("2d"); 
 

 
var startDate = new Date(2016, 07, 01, 00, 00, 00); 
 

 
var currentDate = 0; 
 

 
var valuePerSec = 0.00088776157; 
 
var valuePerSecTax = 0.00307364672; 
 

 
var todaysDate = new Date(); 
 
var todaysWorkStart = new Date(todaysDate.getFullYear(), todaysDate.getMonth(), todaysDate.getDay(), 09, 00, 00); 
 
var yesterday = new Date(todaysDate.getFullYear(), todaysDate.getMonth(), todaysDate.getDay() - 1, 09, 00, 00); 
 

 
//Loops through to draw the graphics. this function is called through the recursiveAnim function 
 
function mainLoop() { 
 
    clearScreen(); 
 
    var currentTime = new Date().getTime(); 
 
    var timeSinceDate = Math.floor((currentTime - startDate.getTime())/1000); 
 
    var yesterdaySinceDate = Math.floor((yesterday.getTime() - startDate.getTime())/1000); 
 
    //var daysSinceBegin = // 
 

 
    textPart(timeSinceDate, yesterdaySinceDate); 
 
} 
 

 
function textPart(timeSinceDate, yesterdaySinceDate) { 
 
    ctx.fillStyle = "#008000"; 
 
    ctx.font = "70px Helvetica"; 
 
    ctx.fillText("£" + (timeSinceDate * valuePerSec).toFixed(2), 150, 260); 
 
    ctx.fillText(Math.round(yesterdaySinceDate/86400), 250, 150); 
 

 
    ctx.font = "20px Helvetica"; 
 
    ctx.fillText(yesterday, 150, 400); 
 
    ctx.fillText("money", c.width - 175, c.height - 6); 
 
} 
 

 
function clearScreen() { 
 
    ctx.fillStyle = "#33cc33"; 
 
    ctx.fillRect(0, 0, c.width, c.height); 
 
} 
 

 
//This loops the animation frames for creating animation 
 
function recursiveAnim() { 
 
    mainLoop(); 
 
    animFrame(recursiveAnim); 
 
} 
 
animFrame(recursiveAnim);
<html> 
 
    <head> 
 
    <title>money</title> 
 
    </head> 
 
    <body> 
 
    <canvas id="canvas" width="600" height="500" style="border:1px solid #000000"></canvas> 
 
    <script src="game.js"></script> 
 
    </body> 
 
</html>

これは私が9時00分の時間の間、毎秒だけ増分に「valuePerSecTax」したい、「valuePerSec」はその日の秒ごとにカウント - 5時30分日常とそれを追加'startDate'以来毎日獲得している金額に相当します。

+0

そして、問題は何ですか? – AxelH

+0

@AxelHオリジナルを更新しました。 –

+0

あなたのコードには何の条件も見当たらないので、あなたが「支払った時間」にいるかどうかを実際に確認することはできません。あなたは解決策について考えましたか? – AxelH

答えて

0

ハンマーを使って蟻を殺そうとしているようですが...計算を行うためのロジックは実装しませんが、コードをどのように構造化するかについてのアイデアは得られます。

//new Date().getTime will return timestamp something like 1470404780537 
 
    
 
//Do calculation every 10 miliseconds 
 
setInterval(calculateIncrease(new Date().getTime()), 10); 
 

 
//Function do to what ever income increase 
 
function calculateIncrease(time_stamp) { 
 

 
    if (is_valid(time_stamp) { 
 

 
     //do calculations 
 
     } 
 
     //else wait 
 
    } 
 

 
    function is_valid(time_stamp) { 
 

 
     //write the logic to check if the time is between 9:30 am and 5:30 pm 
 
    }

関連する問題