I'm trying to make a program that makes a chrome page light up gradually as the desired time is approached, so that it mimics sun light, and I can wake up using monitor's light more successfully.
I'm using p5.js and my codes are as follows.
index.html :
<!DOCTYPE html>
<html>
<head>
<script src='../p5.min.js'></script>
<script src='sketch.js'></script>
</head>
<body style="margin:0;">
</body>
</html>
sketh.js :
function setup(){
createCanvas(1920, 1076);
background(0);
}
function draw(){
var steps = 5; // How many steps of brightness I want
rectMode(CORNERS)
fill(random(0, 255), random(0, 255), random(0, 255), 40)
window.setInterval(function(){ // Set interval for checking
// I added additionally from here ---------------------------------------------
setTimeout(function() {
window.location.reload(false);
}, 2000)
// Up to here -----------------------------------------------------------------
var date = new Date();
var hours = 1; // Hour that I want to wake up
var minutes = 55; // Minute that I want to wake up
var wholeThing = hours * 60 + minutes;
var currentWholeThing = (date.getHours() * 60 + date.getMinutes());
var diff = wholeThing - currentWholeThing;
if ( diff <= steps && diff >= 0)
{
var color = 1 - (diff / steps);
console.log(color);
background(Math.floor(255 * color), Math.floor(205 * color), Math.floor(153 * color))
}
console.log(date.getHours(), date.getMinutes(), date.getSeconds())
}, 6000);
}
And I downloaded p5.min.js from here and added the file in the upper directory from these two source files.
And finally, I used code from SO posts here for the idea of capturing the hour and minute. But I ran into problem that the main function for checking time and choosing color executed too much. Duplicate final console.log appeared many times at each second.(Not to mention I intended that the whole function executing every 6 seconds)
So I looked up here to solve the problem, and I added the code between '//-----------------' comments. but it seems it didn't work. At first it seems like it works in a sense that it refreshes all the logs ones in 2 or 3 seconds(even though duplicate logs appear multiple times during the period) and when the if statement is met even this feature disappears. Finally chrome ate all the memories and it froze.(I don't see why it started to use so much memory though)
How can I make my function to execute just ones in every 6 seconds so that it doesn't waste computing power?