在一天中的某些时段增加数字javascript

时间:2016-08-05 13:34:07

标签: javascript

非常希望增加每天9点到5点30分的赚钱金额,从工作开始就保持其价值。

所以从本质上来说,如果它在晚上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>
&#13;
&#13;
&#13;

这会计算每天的每一秒的价值和价值,我想要&#39; valuePerSecTax&#39;只能在每天9:00到5:30之间每秒递增一次,并将其加到自“开始日期”以来每天赚取的金额中。

1 个答案:

答案 0 :(得分:0)

我不确定,但似乎你正在尝试使用锤子杀死一只蚂蚁......我不会实现任何逻辑来进行计算,但会让你知道如何构建你的代码。

&#13;
&#13;
//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
    }
&#13;
&#13;
&#13;