JavaScript 用 jQuery 加载内容,有 10% 的概率显示不完全

5night · March 10, 2014 · Last by 5night replied at March 10, 2014 · 1904 hits

最近在写一个日程的功能,其中显示的日程内容是由 jQuery 生成的,正常情况下应该是这样: 但制作过程中发现,有大约 10% 概率的概率会出现这样的结果:

日程会少一个,而且少哪个也不确定。 应该是 jQuery 加载不完全的缘故,但不知道如何避免,请问大家有没有遇到过类似的问题,或者如何才能找到原因?

简化版相关代码:

add_calendar = (info, order) ->
    jQuery('<div/>'
    {
            #属性设定 
    }).appendTo('.' + day_class)

$ ->
    for each,i in $('.calendar-info')
        add_calendar($('.calendar-info').eq(i), i)

完整版相关代码:

add_calendar = (info, order) ->
    top = cal_height(info.attr('data-start_time').split(' ')[1])
    height = cal_height(info.attr('data-end_time').split(' ')[1])
    top_str = 'top:' + top.toString() + 'px;'
    height_str = 'height:' + (height - top).toString() + 'px;'
    line_height_str = 'line-height:' + (height - top).toString() + 'px;'
    day_array = ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']
    #每周事件
    if info.attr('data-frequency') == '1'
        start = parse_day($.trim($('.calendar-time').text()).split(' ')[0])
        cal_day = parse_day(info.attr('data-date'))
        for i in [0..4]
            if (((start - cal_day) / 86400000) % 7 == 0) && (cal_day <= start)
                day_class = day_array[cal_day.getDay()]
                break
            else
                start.setDate(start.getDate() + 1)
    #仅此一次
    else if info.attr('data-frequency') == '0'
        day_class = info.attr('data-date').split('-')[1] + '-' + info.attr('data-date').split('-')[2]
    #每双周
    else if info.attr('data-frequency') == '2'
        start = parse_day($.trim($('.calendar-time').text()).split(' ')[0])
        cal_day = parse_day(info.attr('data-date'))
        for i in [0..4]
            if (((start - cal_day) / 86400000) % 14 == 0) && (cal_day <= start)
                day_class = day_array[cal_day.getDay()]
                break
            else
                start.setDate(start.getDate() + 1)
    jQuery('<div/>'
    {
            rel: 'external'
            class: ('thing' + (order % 7 + 1).toString() + ' calendar' + order + ' ' + info.attr('data-source'))
            style: (top_str + height_str + line_height_str + 'cursor:pointer;')
    }).appendTo('.' + day_class)

    jQuery('<div/>'
    {
            class: 'thing-text calendar-text' + order
            text: (info.attr('data-title'))
    }).appendTo('.calendar' + order)
    jQuery('<div/>'
    {
            class: 'attach'
            text: info.attr('data-location')
    }).appendTo('.calendar-text' + order)


$ ->
    for each,i in $('.calendar-info')
        add_calendar($('.calendar-info').eq(i), i)

多测试了几次,发现原因在于,刷新的太快了 T_T,间隔 5 秒以上再次刷新了 20 次,问题没有复现。 暂时先这样吧,刷新频率如我这样的毕竟是少数……

You need to Sign in before reply, if you don't have an account, please Sign up first.