语言

Menu
Sites
Language
Does the Sample project have a bug? 'DigitalWatch'

The sample project 'DigitalWatch' has the following code:

function updateDate(prevDay) {
        var datetime = tizen.time.getCurrentDateTime(),
            nextInterval,
            strDay = document.getElementById("str-day"),
            strFullDate,
            getDay = datetime.getDay(),
            getDate = datetime.getDate(),
            getMonth = datetime.getMonth();

        // Check the update condition.
        // if prevDate is '0', it will always update the date.
        if (prevDay !== null) {
            if (prevDay === getDay) {
                /**
                 * If the date was not changed (meaning that something went wrong),
                 * call updateDate again after a second.
                 */
                nextInterval = 1000;
            } else {
                /**
                 * If the day was changed,
                 * call updateDate at the beginning of the next day.
                 */
                // Calculate how much time is left until the next day.
                nextInterval =
                    (23 - datetime.getHours()) * 60 * 60 * 1000 +
                    (59 - datetime.getMinutes()) * 60 * 1000 +
                    (59 - datetime.getSeconds()) * 1000 +
                    (1000 - datetime.getMilliseconds()) +
                    1;
            }
        }

        if (getDate < 10) {
            getDate = "0" + getDate;
        }

        strFullDate = arrDay[getDay] + " " + getDate + " " + arrMonth[getMonth];
        strDay.innerHTML = strFullDate;

        // If an updateDate timer already exists, clear the previous timer.
        if (timerUpdateDate) {
            clearTimeout(timerUpdateDate);
        }

        // Set next timeout for date update.
        timerUpdateDate = setTimeout(function() {
            updateDate(getDay);
        }, nextInterval);
    }

1. At first the method 'updateDate(prevDay)' is called with 0 parameter;

2. In the end this method sets a timeout with the particular time interval to call itself again but with the other parameters (0 'Sun', 1 'Mon', 2 '...', 3, 4, 5, 6);

If the application is started on Monday (1 is a parameter for Monday) the method sets the correct time interval (23:59:59:1000 minus actual time) however the wrong time interval (1 second) is set if the application is started on Sunday (0 is a parameter for Sunday), so this updateDate(prevDay) method is called every second the whole day until the next day.

 

So I've changed the argument to -1 and the application works fine now.

I don't think it is normal. How do you think?

编辑者为: Anonymous 03 5月, 2019

响应

1 回复
Onur Şahin

You are right that function is keep calling itself with 1000 ms interval if the current day is Sunday. My guess is someone with C background forgot he/she is writing in Javascript and thought if he pass 0 as prevDay, if (prevDay !== null) will return false, as the comment states;

// Check the update condition.
// if prevDate is '0', it will always update the date.
if (prevDay !== null) { /* ... */ }

 

For what it's worth, i think all that set an interval to update date thing is an early optimization to get rid of just one if condition in updateTime() function. Most time consuming method in these two functions is tizen.time.getCurrentDateTime() and since we call it in updateTime() anyway, putting this if block inside the updateTime() function should not be a problem.

var oldDate; //global

/** ... */

var today;

if((today = dateTime.getDate()) !== oldDate){
    //move the contents of updateDate() here, without setTimeout part
    oldDate = today;
}