Dawn Break Alarm Timepiece - Online Platform

My buddy recently purchased a "sunrise alarm clock" that gradually brightens to mimic a sunrise and make waking up easier.

I had the thought of replicating this effect with my laptop, but I've been facing issues with getting the JavaScript time function to work correctly. Any assistance in making this concept a reality would be greatly appreciated.

I've been attempting to adjust the minutes value (wakeupM) based on how long I intend to sleep, aiming to delay the setTimeout function until it's time to wake up. However, my goal is to set a specific time in the input box and have the "sunrise" feature activate only when that time arrives.

Thanks in advance for your help!

var countTime;

function mySleep() {
  var now = new Date();
  var nowM = now.getMinutes();

  var wakeup = new Date();
  var wakeupM = wakeup.getMinutes() + 3;

  var timeDif = wakeupM - nowM;
  countTime = 6000 * timeDif;

  document.getElementById("settime").innerHTML = countTime;

}

function mySunrise() {
  var colors = ['orange', 'yellow', 'white'];
  var active = 0;

  setInterval(function() {
    document.querySelector('body').style.background = colors[active];
    active++;
    if (active == colors.length) active = 0;
  }, 3000);;
}
body {
  background: green;
  /* initial color */
  transition: background 5s;
  /* .5s how long transition should take */
}
<input type="time" id="myTime" value="22:15:00">
<button onclick="setTimeout(mySunrise, countTime); mySleep();">Wake me up</button>
<h1>waking you up at:</h1>
<p id="settime"></p>

Answer №1

Visit the completed version on my personal website -

var wakeupH;
var wakeupM;
var wakeupS;

var nowH;
var nowM;
var nowS;


function mySleep() {
  var wakeUpTime = document.getElementById("myTime");
  var currentVal = wakeUpTime.value;
  var res = currentVal.split(":");

  wakeupH = res[0]
  wakeupM = res[1]
  wakeupS = res[2]

  var now = new Date();
  var nowH = now.getHours();
  var nowM = now.getMinutes();
  var nowS = now.getSeconds();

  var date1 = new Date(2000, 0, 1, nowH, nowM, nowS);

  var date2 = new Date(2000, 0, 1, wakeupH, wakeupM, 1);


  if (date2 < date1) {
    date2.setDate(date2.getDate() + 1);
  }

  countTime = date2 - date1;
  document.getElementById("settime").innerHTML = countTime;
}



function mySunrise() {
  var colors = ["#BDB76B", "#F0E68C", "#EEE8AA", "#FFDAB9", "#FFE4B5", "#FFA07A", "#FF7F50", "#FF6347", "#FF4500", "#FF8C00", "#FFA500", "#FFEFD5", "#FAFAD2", "#FFFACD", "#FFFFE0", "#FFFF00", "#FFD700", "white"];
  var active = 0;


  setInterval(function() {
    document.querySelector('body').style.background = colors[active];
    active++;
    if (active == colors.length) active = 0;
  }, 3000);;
}
body {
  background: black;
  /* initial color */
  transition: background 30s;
  /* .5s how long transition should take */
  color: white;
}
Time: <input type="time" id="myTime" value="22:53:01">
<button onclick="mySleep(); setTimeout(mySunrise, countTime);">Rise and shine</button>
<p id="settime"></p>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Having trouble dividing the Ajax response into individual Div elements

I'm looking to split the Ajax response into two separate Divs instead of receiving it in one Div. Below is the code I am currently using: $(document).ready(function() { $.post("check.php", { job: exactdatainner, attrb: getattr }, ...

Using Jquery to select a specific id for an input element that is a checkbox

I've been working on a snippet of jQuery code that I'd like to tailor to be more specific for one of two different input elements on my HTML page. <script> // ensure only one box is checked at a time $(document).ready(function() { ...

Notify users with a prompt when a modal or popup is closed on Google Chrome extensions

I have developed a Google Chrome extension for setting timers and receiving alerts. Currently, the alert only goes off when the extension is open, but I want it to fire even when the extension is closed. This extension currently requires the window to be ...

How to modify the color of a div element with jQuery?

I need some help changing the color of a div with 'jQuery', but I am not sure how to do it. Below is the code I have been trying: function updateDivColor() { $('#OutlineX1').css("color","blue"); } ...

The discord.js argument for startsWith should not be a standard regular expression

https://i.sstatic.net/UG79z.png What could be the reason behind this not working as expected? I am trying to check if a string starts with a number, and furthermore, I want it to handle multiple numbers. For instance, if the string starts with 1259823 the ...

What is the best way to incorporate the value of a textbox into a list?

I currently have a list structured like this: <p>Please choose from the following options:</p> <script type="text/javascript"></script> <select id='pre-selected-options1' multiple='multiple'> <opt ...

Add fresh inline designs to a React high-order component creation

Applying a common HOC pattern like this can be quite effective. However, there are instances where you may not want a component to be wrapped, but rather just extended. This is the challenge I am facing here. Wrapper HOC const flexboxContainerStyles = { ...

Listener for body keystrokes

Is there a way to trigger a function when the space bar is pressed on the page, without it being called if an input field is focused? Any thoughts or suggestions? The current code triggers the function even when an input bar is focused: $(document).keydo ...

Go back to initial value loaded from ajax when canceling the action

On my webpage, I have both a display view (using span) and an edit view (using input) visible to the user. The user can switch between these views using buttons. In the Display View, there is an Edit Button that allows the user to switch to the Edit View. ...

Guide on importing table information into an array using jQuery

I am facing an issue where I want to extract values from a dynamically generated table and then send those values in an AJAX call. The problem is that even though I am able to increase the number of rows in the table dynamically, when I try to capture the ...

mongodb cannot locate the schema method within the nested container

Trying to access a method of a schema stored inside a mixed container has presented a challenge. The scenario is as follows: var CaseSchema = mongoose.Schema({ caseContent : {}, object : {type:String, default : "null"}, collision : {type : Boo ...

Unusual occurrences observed with the table

While working with tables, I've noticed an unusual effect. Take a look at the two fiddles below. http://jsfiddle.net/v5co5uy7/ (why are the cells in the left column pushed down?) http://jsfiddle.net/v5co5uy7/1/ (the cells in the left column are posi ...

Is there a way to undo the click event in Javascript?

Is there a way to revert back to the initial position after clicking? I successfully changed the x icon to a + icon on click, but now I'm struggling to reverse that action. I've tried using MOUSEUP and DBLCLICK events, but they aren't ideal ...

Is it possible to extract elements from a single list and insert them onto various pages?

Currently, I am retrieving items from a list using an ajax call. After constructing the HTML content, I insert these items onto a page. Now, I want to extract the same items and display them on another page with a different style. Is there a method to conn ...

Creating a full-width tab card with Bootstrap-Vue: A step-by-step guide

I stumbled upon something similar in the documentation for bootstrap-vue: A card with tabs: Now, how can I style the tabs to look like this: This is my current code: <b-card no-body> <b-tabs card> <b-tab title="Tab 1" active& ...

Using an xmlhttprequest to retrieve a PDF file functions properly in synchronous mode, but encounters issues in

Today I'm experimenting with some code that scrapes PDFs from a chrome extension by detecting user-clicked links. I've noticed that synchronous xmlhttprequests work perfectly for both HTML documents and PDFs. However, I seem to be facing an issue ...

Can you group polygons in layers using Phaser?

My goal is to animate two polygons: one that remains stationary and another that changes shape while moving. The challenge is to ensure that the stationary polygon always stays on top when overlapping. This is my proposed solution: (function() { "use ...

Experience the sleek transparency when black hues grace the interface on iOS 14 and above

When implementing the code snippet below, .packages > .ulWrapper::after { background: linear-gradient( to left, var(--edge-color) 5%, rgba(0, 0, 0, 0) 95% ); /* option 1 - red */ background: linear-gradient( to left, var(--edg ...

How can the dropdownlist CSS be modified when it is clicked or hovered over?

I recently came across a solution on how to enable a disabled dropdown list when clicked at . I attempted to implement this and added a grey background to the disabled selection. However, I encountered an issue where the background of the dropdown list re ...

Issue encountered while attempting to attach an event listener to an element within a class

Upon running the code, I encountered an error message that reads: Uncaught TypeError: this.startButton.addEventListener is not a function and I'm unsure of how to resolve it. Although I can successfully console.log the button inside the class, adding ...