简体   繁体   English

if和else语句都一起执行

[英]Both the if and else statement are executing together

I've modified this code for the example so the date and time isn't currently updating on it's own. 我已为示例修改了此代码,因此日期和时间当前不会自行更新。 The basic explanation is: There is a counter keeping track of the current time and refreshing every second, pushing it to a element in the DOM . 基本解释是:有一个计数器跟踪当前时间并每秒刷新一次,将其推送到DOM的元素。 However I want to execute code for when the time is between two certain times of the day. 但是我想为时间在一天中的两个特定时间之间执行代码。 These times were defined in the setDrwBrks function like this: 这些时间是在setDrwBrks函数中定义的,如下所示:

  mBrkStrt.setHours( 9, 50, 0 ); mBrkEnd.setHours( 10, 3, 0 );

  dBrkStrt.setHours( 12, 17, 0 ); dBrkEnd.setHours( 12, 30, 0 );

  eBrkStrt.setHours( 17, 50, 0 ); eBrkEnd.setHours( 18, 3, 0 );

  nBrkStrt.setHours( 22, 2, 0 ); nBrkEnd.setHours( 22, 15, 0 );

This is my problem code in the fndNxtDrwBrk( date ) function: 这是我在fndNxtDrwBrk( date )函数中的问题代码:

  for( j = 0; j < 4; j++ ) {
    if( date > strtDrwBrks[ j ] && date < endDrwBrks[ j ] ) {
      dspl.textContent += '... First statement executed';
    }
    else {   
      document.querySelector( 'footer' ).textContent = 'why is this also executed?';
    }
  }

The goal here is to execute the if statement when the date time is between the strtDrwBrks[ j ] and endDrwBrks[ j ] times. 这里的目标是在date时间介于strtDrwBrks[ j ]endDrwBrks[ j ]时间之间时执行if语句。 In my snippet this executes because I've manually set the time to be 9:51AM here in my pushDates() function: 在我的代码段中,这是因为我已经在我的pushDates()函数中手动将时间设置为9:51 AM:

  ////Control current time 
  today.setDate( 8 );
  today.setHours( 9 );
  today.setMinutes( 51 );

So between 9:50am and 10:03am this code should be executed, which it does. 因此,在9:50 am到10:03 am之间,应执行此代码,而且确实如此。 However my else statement also executes. 但是我的else语句也会执行。 This is the problem. 这就是问题。 Any ideas why this happens and how to fix it? 有什么想法为什么会发生以及如何解决? I've been working at this for hours to no avail. 我已经在这工作了好几个小时了。

 'use strict'; // ///////////////////////////// INITIAL /////////////////////////////////// // function leading_0( num ) { if( num < 10 ) { num = '0' + num; } return num; } // ////////////////////////////// DATES //////////////////////////////////// // function getCurrentTime( date ) { // TIME / / / / / / / / / / / / / / / / / // var hours = date.getHours(), minutes = date.getMinutes(), seconds = date.getSeconds(), suffix = hours >= 12 ? 'PM' : 'AM', fullTime; hours = hours % 12; if( hours === 0 ){ hours = 12; } minutes = leading_0( minutes ); seconds = leading_0( seconds ); fullTime = hours + ':' + minutes + ':' + seconds + ' ' + suffix; return fullTime; } // \\\\/ / / / / / / / / / / / / / / TIME / / / / / / / / / / / / / / / / / // function getYear( date ) { /// / / / YEAR / / / / / / / / / / / / / / / / / // var year = date.getFullYear(); return year; } // \\\\/ / / / / / / / / / / / / / / YEAR / / / / / / / / / / / / / / / / / // function getMonthDay( date ) { /// MONTH DAY / / / / / / / / / / / / / / / /// var day = date.getDate(); return day; } // \\\\/ / / / / / / / / / / / / / MONTH DAY / / / / / / / / / / / / / / / /// function getMonth( date ) { // / / / MONTH / / / / / / / / / / / / / / / / /// var months = [ 'January', 'Feburary', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December' ], month = months[ date.getMonth() ]; return month; } // \\\\/ / / / / / / / / / / / / / / MONTH / / / / / / / / / / / / / / / / /// function getWkDay( date ) { /// / / WEEK DAY / / / / / / / / / / / / / / / /// var weekdays = [ 'Sunday', 'Monday', 'Tueday', 'Wednesday', 'Thursday', 'Friday', 'Saturday' ], wkDay = weekdays[ date.getDay() ]; return wkDay; } // \\\\ / / / / / / / / / / / / / / WEEK DAY / / / / / / / / / / / / / / / /// function callBySec( func ) { setInterval( func, 1000 ); } function pushDate() { // / / / / / PUSH DATES / / / / / / / / / / / / / / / // var today = new Date(), wkDay, month, day, year, time, d = document; ////Control current time today.setDate( 8 ); today.setHours( 9 ); today.setMinutes( 51 ); wkDay = getWkDay( today ); month = getMonth( today ); day = getMonthDay( today ); year = getYear( today ); time = getCurrentTime( today ); d.getElementById( 'wkDay' ).textContent = wkDay; d.getElementById( 'month' ).textContent = month; d.getElementById( 'day' ).textContent = day; d.getElementById( 'year' ).textContent = year; d.getElementById( 'time' ).textContent = time; return today; } // \\\\/ / / / / / / / / / / / / / PUSH DATES / / / / / / / / / / / / / / / // function nextDate( startDate, dates ) { // NEXT DATE / / / / / / / / / / / / / var startTime = + startDate, nearestDate, nearestDiff = Infinity, i, n, diff; for( i = 0, n = dates.length; i < n; ++i ) { diff = + dates[ i ] - startTime; if( diff > 0 && diff < nearestDiff ) { nearestDiff = diff; nearestDate = dates[ i ]; } } return nearestDate; } // \\\\ / / / / / / / / / / / / / / / / / NEXT DATE / / / / / / / / / / / / // function setDrwBrks() { // / / / SET DRAW BREAKS / / / / / / / / / / / / / / / var today = pushDate(), mBrkStrt = pushDate(), mBrkEnd = pushDate(), dBrkStrt = pushDate(), dBrkEnd = pushDate(), eBrkStrt = pushDate(), eBrkEnd = pushDate(), nBrkStrt = pushDate(), nBrkEnd = pushDate(), strtDrwBrks = [], endDrwBrks = [], drwBrksColl = []; mBrkStrt.setHours( 9, 50, 0 ); mBrkEnd.setHours( 10, 3, 0 ); dBrkStrt.setHours( 12, 17, 0 ); dBrkEnd.setHours( 12, 30, 0 ); eBrkStrt.setHours( 17, 50, 0 ); eBrkEnd.setHours( 18, 3, 0 ); nBrkStrt.setHours( 22, 2, 0 ); nBrkEnd.setHours( 22, 15, 0 ); strtDrwBrks.push( mBrkStrt ); endDrwBrks.push( mBrkEnd ); strtDrwBrks.push( dBrkStrt ); endDrwBrks.push( dBrkEnd ); strtDrwBrks.push( eBrkStrt ); endDrwBrks.push( eBrkEnd ); strtDrwBrks.push( nBrkStrt ); endDrwBrks.push( nBrkEnd ); drwBrksColl.push( strtDrwBrks ); drwBrksColl.push( endDrwBrks ); return drwBrksColl; } // \\\\/ / / / / / / / / / / / / SET DRAW BREAKS / / / / / / / / / / / / / /// function fndNxtDrwBrk( date ) { // FIND NEXT DRAW BREAK / / / / / / / / / / /// date = pushDate(); var drwBrks = setDrwBrks(), nxtDrwBrk = nextDate( date, drwBrks[ 0 ] ), strtDrwBrks = drwBrks[ 0 ], endDrwBrks = drwBrks[ 1 ], i, j, day, dspl = document.getElementById( 'dspl' ), dspl2 = document.getElementById( 'dspl-2' ); if( nxtDrwBrk === undefined ) { for( i = 0; i < 4; i++ ){ drwBrks[ 0 ][ i ].setDate( date.getDate() + 1 ); } nxtDrwBrk = nextDate( date, drwBrks[ 0 ] ); } day = nxtDrwBrk.getDay(); if( day === 0 ) { nxtDrwBrk.setDate( nxtDrwBrk.getDate() + 1 ); nxtDrwBrk.setHours( 9, 50, 0 ); } for( j = 0; j < 4; j++ ) { if( date > strtDrwBrks[ j ] && date < endDrwBrks[ j ] ) { dspl.textContent += '... First statement executed'; } else { document.querySelector( 'footer' ).textContent = 'why is this also executed?'; } } return nxtDrwBrk; } // \\\\ / / / / / / / / / / / / FIND NEXT DRAW BREAK / / / / / / / / / / / /// // ////////////////////////////// START //////////////////////////////////// // function start() { var today = pushDate(); pushDate(); callBySec( pushDate ); fndNxtDrwBrk( today ); } start(); 
 .remove { display: none; } 
 <p id="dspl"> <span id="wkDay"></span>, <span id="month"></span> <span id="day"></span>, <span id="year"></span> <b>|</b> <span id="time"></span> </p> <p id="dspl-2" class="remove"> Secondary Text </p> <footer></footer> 

PS. PS。 The error in the console is because I manually changed the time to a range between the test times with the code under the ////Control current time comment. 控制台中的错误是因为我使用////Control current time注释下的代码手动将时间更改为测试时间之间的范围。 Remove this and the console will be quiet. 删除它,控制台将安静。 No idea how to set the time like that for this snippet without running into that error, tried fixing, but that is a separate problem that doesn't really need to be fixed as the code setting it off is just for this demo. 不知道如何为该代码段设置时间而不遇到该错误,尝试修复,但这是一个单独的问题,实际上不需要修复,因为将其设置为关闭的代码仅用于此演示。

You are iterating between your four differents interval of time. 您正在四个不同的时间间隔之间进行迭代。 The time cannot be between your first interval and the second interval and the third and the fourth at the same time. 时间不能在您的第一个间隔和第二个间隔之间,并且不能同时在第三个和第四个间隔之间。

For example: 12.30 is between 10.10 and 13.20, but not between 15.20 and 16.40 So when it comes to test if it's between the second time interval, it goes to the else statement. 例如:12.30在10.10和13.20之间,但不在15.20和16.40之间。因此,当要测试它是否在第二时间间隔之间时,它将转到else语句。 As you are testing 4 intervals (looping 4 times), it will goes to the else 3 times as only one interval contains the actual hour. 当您测试4个间隔(循环4次)时,它将转到其他3次,因为只有一个间隔包含实际的小时数。

So, let's say your want to know if it's between one of the four interval you have. 因此,假设您想知道它是否在四个间隔之一之间。

Created a boolean isBetweenOneInterval that is set to false by defaut. 创建了一个boolean isBetweenOneInterval ,它被isBetweenOneInterval设置为false。 Then iterate between your interals. 然后在您的插入之间进行迭代。

If time is between interval

Set isBetweenOneInterval to true isBetweenOneInterval设置为true

After the end of your loop, check if isBetweenOneInterval is true . 循环结束后,检查isBetweenOneInterval是否为true

If isBetweenInterval is true do what you want to do when it's between an interval. 如果isBetweenIntervaltrue ,则在间隔之间进行操作。 If not, do what you want to do when it's not between an interval, for example, hide the div containing the countdown. 如果不是,请在间隔之间不进行操作,例如,隐藏包含倒数的div。

Put into code it means : 放入代码中意味着:

var isBetweenOneInterval = false;

 for( j = 0; j < 4; j++ ) {
    if( date > strtDrwBrks[ j ] && date < endDrwBrks[ j ] ) {
     isBetweenOneInterval = true;
    }
  }

if (isBetweenOneInterval == true)
{
 //// DO WHAT YOU WANT IF THE CURRENT TIME IS BETWEEN AN INTERVAL
}
else
{
 ///// DO WHAT YOU WANT IF THE CURRENT TIME ISN'T BETWEEN ANY INTERVAL
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM