douxin2002 2014-07-09 19:57
浏览 75
已采纳

检查时间范围是否重叠,如果为真则触发错误

Sorry for the length of this question, but there is some background you need to know to fully understand:

I am building an event scheduler in Drupal 7 which allows users to check a checkbox to add an event to their schedule... and it's a mess. The checkboxes contain a value looking similar to 1404915600 - 1404917400 which represents 10:30am - 10:50am which is the result of using PHPs strtotime(); function like so strtotime("10:30am");

I need to check on the fly if the user checks another box with an overlapping time as a previously selected event and trigger an error if there is an overlap.

Currently, I am using the php function mentioned above to create the values in the checkboxes, and then passing those values to a javascript array on click of checkbox. The id of the checkbox is the url friendly name of the selected event, which acts as the array key, and the values are the selected times, so the array ends up looking like:

timeArr = { event-name-one: 0: 1404915600, 1: 1404917400 }

Each event has it's own key to simplify deleting (or unscheduling) events using Javascripts delete function.

Here is the complete code which grabs the checkbox ID and the times associated and stores them in the array:

var timeBox = jQuery('input[type="checkbox"].time, input[type="checkbox"].time2');
var timeArr = [];

// Listen for checkbox change
timeBox.bind('click', function() {

    var eventID = jQuery(this).attr('id');

    // If Checkbox IS checked
    if(jQuery(this).prop('checked')) {

        var timeStamp = jQuery(this).val();
        var newTime = timeStamp.split(" - ");

        // If timeArr has been set
        if(timeArr.length > 1) {

            for(key in timeArr) {
                if(newTime[0] > timeArr[key][0] && newTime[0] < timeArr[key][1] || timeArr[key][0] > newTime[0] && timeArr[key][0] < newTime[1]) {
                    alert("Overlap!");
                    return false;
                } else {
                    timeArr[eventID].push(newTime);
                    console.log(timeArr);
                }
            }           

        } else {
            // else timeArr hasn't been set -- so set it!                
            timeArr[eventID] = newTime;
            console.log(timeArr);
        }
    } else {
        delete timeArr[eventID];
    }        
});

I can't seem to get the newly selected event time to cross check with the current times in the array. They just get appended to the timeArr array as if the checks passed.

Am I going about this the complete wrong way or am I just missing something little? I've tried so many different things I'm quite lost as to which is the right direction to take. I am using Drupal 7 if that helps any.

  • 写回答

1条回答 默认 最新

  • doucan8049 2014-07-09 20:57
    关注

    The formula is start1 < end2 && end1 > start2. This will detect overlap in all 4 comparisons needed (below). You also need to decide if the exact start and end time is inclusive or exclusive. I.E. if something ends at 1000 and something else starts at 1000 are they overlapping for that exact 1000 minute?

    The four conditions of overlap are:

    1. Event1 overlaps the start of Event2 (Event1: 10am-12am, Event2: 11am-1pm)
    2. Event1 overlaps the end of Event2 (Event1: 10am-12am, Event2: 9am-11am)
    3. Event1 completely encompasses Event2 (Event1: 10am-12am, Event2: 1030am-1130am)
    4. Event2 completely encompasses Event1 (Event1: 10am-12am, Event2: 9am-1pm)

    The formula above covers all 4 possible scenarios.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 想问一下树莓派接上显示屏后出现如图所示画面,是什么问题导致的
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号