My gadget is caught in a loop, the conditions are failing to function properly

I recently developed a basic web watch application using Tizen Studio specifically designed for Gear watches and similar devices.

Within this web watch, I implemented 6 conditions that are intended to change the background of the watch based on the current time of day. These conditions represent different phases of the day such as early morning, morning, afternoon, early evening, evening, and night.

These conditions are integrated with the digital clock feature of the watch. This setup was necessary for the functionality to work properly. However, I have encountered an issue.

The night condition seems to be malfunctioning, and I'm unable to pinpoint the cause since it follows the same logic as the other daytime conditions. Furthermore, transitioning back to the 'day mode' after nighttime does not seem to be functioning correctly either. Additionally, after a few days, the watch appears to enter into a loop.

JS

window.onload = function() {
// TODO:: Initialization tasks

// variables for image transitions

// add event listener for tizenhwkey
function showTime(){
    var date = new Date();
    var h = date.getHours(); // 0 - 23
    var m = date.getMinutes(); // 0 - 59
    var s = date.getSeconds(); // 0 - 59


    if(h === 0){
        h = 24;
    }

    h = (h < 10) ? "0" + h : h;
    m = (m < 10) ? "0" + m : m;
    s = (s < 10) ? "0" + s : s;

    var time = h + ":" + m + ":" + s;
    document.getElementById("MyClockDisplay").innerText = time;
    document.getElementById("MyClockDisplay").textContent = time;

    // Daytime transitions

    if('10:00:05' <= time && time < '16:00:00')
    {
        document.getElementById("day1").style.opacity = "0";
        document.getElementById("day2").style.opacity = "1";
    }   

    if('16:00:05' <= time && time < '20:00:00')
    {
        document.getElementById("day2").style.opacity = "0";
        document.getElementById("day3").style.opacity = "1";
    }  

    if('20:00:05' <= time && time < '23:00:00')
    {
        document.getElementById("day3").style.opacity = "0";
        document.getElementById("night1").style.opacity = "1";
    }   

    if('23:00:05' <= time && time < '04:00:00')
    {
        document.getElementById("night1").style.opacity = "0";
        document.getElementById("night2").style.opacity = "1";
     }   

    if('04:00:05' <= time && time < '06:00:00')
    {
        document.getElementById("night2").style.opacity = "0";
        document.getElementById("night3").style.opacity = "1";
    }  

    if('06:00:05' <= time && time < '10:00:00')
    {
        document.getElementById("nigth3").style.opacity = "0";
        document.getElementById("day1").style.opacity = "1";
    } 

    setTimeout(showTime, 1000);  

}

showTime();

function showdate(){

    var d = new Date();
    var days = ["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"];
    document.getElementById("date").innerHTML = days[d.getDay()];
    }

showdate();

};

HTML

.clock {
position: absolute;
top: 45%;
left: 50%;
transform: translateX(-50%) translateY(-50%);
color: #FFFFFF;
font-size: 30px;
/*   font-family: Orbitron; */
letter-spacing: 7px;
}

img {
position: fixed;
top: 0%;
left: 0%;
height: 360px;
width: 360px;
transition: all 5s ease;
}

#components-main {
position: absolute;
width: 100%;
height: 100%;
}

.showsPM {
 position: absolute;
 top: 60%;
 left: 50%;
 transform: translateX(-50%) translateY(-50%);
 color: #FFFFFF;
 font-size: 28px;
 /*   font-family: Orbitron; */
 letter-spacing: 7px;
 }

.showsDate {
 position: absolute;
 top: 55%;
 left: 50%;
 transform: translateX(-50%) translateY(-50%);
 color: #FFFFFF;
 font-size: 22px;
 /*   font-family: Orbitron; */
 letter-spacing: 5px;
 }
 

HTML

            <script src="js/main.js"></script>
            </head>

            <body>
            <div id="container">    


    <img id="day1" src="/images/day1.png" style="opacity: 0"/>   
    <img id="day2" src="/images/day2.png" style="opacity: 1"/>  
    <img id="day3" src="/images/day3.png" style="opacity: 0"/>  
    <img id="night1" src="/images/night1.png" style="opacity: 0"/>  
    <img id="night2" src="/images/night2.png" style="opacity: 0"/>  
    <img id="night3" src="/images/night3.png" style="opacity: 0"/>  

    <!--  <div id="backgroundNight" style="opacity: 0;"></div>-->

        <div id="MyClockDisplay" class="clock" onload="showTime()"></div>
        <div id="date" class="showsDate"></div>     

</div>
<script src="js/main.js"></script>

Are there any alternative approaches I could take to achieve the desired functionality? Any suggestions or observations regarding my code?

Answer №1

The issue lies within this particular line of code

if('23:00:05' <= time&&time < '04:00:00')
. The problem here is that the condition will never be met as it states that time should be greater than 23 and less than 04.

The comparison operator does not recognize that 01 comes after 23 in this context. To address this, you can switch the && to || (or) since both values are at the extremities of the range:

if('23:00:05' <= time || time < '04:00:00')

Answer №2

The issue lies within this specific condition:

if('23:00:05' <= time && time < '04:00:00')

For instance, if it is 23:10, the first part of the condition holds true while the second part does not, as expected given that 23 is not less than 4.

To rectify this, modify the condition as follows:

if('23:00:05' <= time || time>='00:00:05' && time < '04:00:00')

This adjustment ensures that the condition is met between the hours of 11 PM to 12 AM and 0 AM to 4 AM.

Answer №3

This particular scenario will never ring true.

if('23:00:05' <= time&&time < '04:00:00')
{
    document.getElementById("night1").style.opacity = "0";
    document.getElementById("night2").style.opacity = "1";
}

Take this alternative action instead

if(('23:00:05' <= time&&time <= '24:59:59') || ('01:00:00' <= time&&time < '04:00:00'))
{
    document.getElementById("night1").style.opacity = "0";
    document.getElementById("night2").style.opacity = "1";
}

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

Exploring the Depths of MongoDB Arrays

Below is the structure of my MongoDB data: { "_id": "QEvgJKERy5xGNLv7J", "title": "test 2", "owner": "HQNGYZvrrdQRwLNvT", "markdown": "sdfasdfdasf adsfadsfasdf", "state": "Draft", "category": [ "Bios & Memoirs", "Classics" ], " ...

What is the reason behind event bubbling failing to function properly when CSS grid is utilized and elements are placed in the same cell

I am facing an issue with two grid children occupying the same cell and both having click event listeners. As per the concept of event bubbling, I expect both event listeners to be triggered when I click on the cell. const outer = document.getElementByI ...

Is it possible to assign a width property to a div element?

I want DivTest and IdOtherDIV to have the same width. I attempted to set properties like this: DivTest { background: #007C52; width: document.getElementById("IdOtherDIV").scrollWidth + "px.\n"; } Is there a way to achieve this (considering tha ...

Looking for a way to efficiently retrieve results by matching multiple string keywords as you go through each line of a file (fs)?

Essentially, I have multiple search strings provided by the client that need to be matched with each line in a file. If a line matches all of the inputted strings, I should add that line to the results array. However, when I run the code below, it only ret ...

Simply looking to activate keyup, keydown, or space events using jQuery/JavaScript

What is the method to activate a keyup event? Imagine having a text field with the id "item" and triggering an event with a space should result in adding a space to the text field. I am interested in triggering this event solely from the JavaScript consol ...

Inject the HTML code into a bash script and perform operations on it

Could someone help me with a bash scripting question? I'm new to this, so I may have missed it if it's already been answered. I want to pass the html source code of a web page to my script, which will then modify or scrape the web page of its HT ...

Send HTML table information from view to Controller action in ASP.NET Core by converting it to a list

Encountering issues with null values in the controller action method, preventing table data rows from being looped through. Model: class: public class Generation { public int Generation1Count { get; set; } public int Generation1TotalS ...

Utilizing AngularJS to access one route's resolve data from another resolve

I am working on a route resolve that includes the following functions: resolve: { user: function($route, User){ return User.find($route.current.params.id); }, stats: function($route, Stats) { /* asynchronous stat retrieva ...

The React application is experiencing difficulty in rendering SVG images exclusively on Windows operating systems

My react app runs smoothly on OSX, but encounters issues on Windows due to SVG files: Module parse failed: Unexpected token (2:0) You may need an appropriate loader to handle this file type. <svg xmlns="http://www.w3.org/2000/svg" viewBox="..."> I ...

What is the best way to make a div span the entire height of the body?

I have a menu that I want to wrap inside a yellow div. The yellow div should take up the entire height of the green square, regardless of the content size in the green square. For example, if the orange square expands, the yellow div should also expand. h ...

Exploring the concept of Variable Scope within a jQuery promise

Picture yourself executing the following code snippet within a JavaScript function named Fetch. function Fetch(context) { var request = $.ajax({...}); request.done(function(response) { // It appears that context is accessible here and in scope. ...

Which directives in AngularJS facilitate one-way binding?

Which directives in AngularJS support one-way binding? While ng-model enables two-way binding, what about ng-bind and {{ }} expressions - do they also support one-way binding? ...

XML view does not support data-binding functionality

I have encountered a problem with data visualization when using the XML view compared to the JSON view in a simple example. The binding works fine with JSON, but no data is visualized with XML. Why is that? This code snippet represents the controller logi ...

The ng-change functionality of Angular radio buttons only functions correctly the first time it

I am struggling to comprehend why the angular ng-change function is only being called on the first click. Take a look at this example: http://jsfiddle.net/ZPcSe/5/ The function in the provided example is triggered every time the radio selection is change ...

How can we prevent an ajax call from being made when the user input is empty?

Currently, I am utilizing the keyup event for an Ajax call on an input field along with the f:validaterequired tag. Each time the Ajax event (keyup) triggers, f:validaterequired is being validated. However, I do not want to validate when the value is null. ...

What is preventing my accordion from closing completely?

I'm currently working on creating FAQ questions in an accordion format. However, I've encountered an issue where adding padding around the answer section prevents the accordion from closing completely. Removing the padding solves the problem, but ...

Alpha Screen and Shading Filter

Having trouble with CSS filters in IE8. I have a div with a gradient background that needs to have opacity set to 0, and when hovered over, the opacity should change to 1. Here's my code: #myDiv { filter: alpha(opacity=0); opacity: 0; background:rgba ...

Implementing and styling jQuery hamburger navigation menu

I'm struggling with implementing functionality to my hamburger menu. I've spent a day working on the design, but now I need it to actually work as intended. I have tried combining different code snippets I found, but as I am new to jQuery, I&apos ...

Tips for concealing specific sections or content on a tab

I am looking to implement a layout with 4 tabs where the content of each tab is initially hidden and only becomes visible when that specific tab is clicked. For example, if the user clicks on the 1st tab, then the content associated with that tab should ...

The relative positioning is currently being obstructed

Having some trouble using position:relative to shift my logo to the left of my webpage. Oddly, moving it 20px seems to have no effect, but shifting it by 300px downwards causes it to vanish completely. Here's a snippet of my current code: .container ...