2016-08-05 3 views
-1

매일 9시에서 5시 30 분 사이에 벌어 들인 돈을 늘리려고합니다.하루 중 특정 시간 동안 숫자가 증가합니다.

그래서 오늘 밤 5시 30 분 밤에 '돈 벌기'가 9시 30 분까지 중지되고 5시 30 분까지 초당 증가합니다.

여기까지 지금까지의 내용이 있습니다. 및 추가 5시 반 일상 -

//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의 시간 사이 초당 증가에 'valuePerSec'와 함께 하루의 매 순간, 나는 'valuePerSecTax'을 원하는 수를 계산 '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 
 
    }

관련 문제