weixin_39702649
weixin_39702649
2020-11-23 09:09

elements[elements.length-1] sometimes undefined

I'm not entirely sure how this occurs, but in the setup at my work's website using jQuery 2, a fatal JS error is thrown, meaning the overlays don't show at all. This is the offending code:


if ( elements[ elements.length-1 ].listeners.length === 0 ) {
        elements.splice( elements.length-1, 1 );
}

To fix it, we just need to change it to the following:


if ( typeof elements[ elements.length-1 ] != 'undefined' && elements[ elements.length-1 ].listeners.length === 0 ) {
            elements.splice( elements.length-1, 1 );
}

I absolutely love visual event!

该提问来源于开源项目:DataTables/VisualEvent

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

6条回答

  • weixin_39932611 weixin_39932611 5月前

    Thanks :-)

    Are you able to link me to the page so I can tae a look at what is going on and ensure that I fix it correctly?

    点赞 评论 复制链接分享
  • weixin_39702649 weixin_39702649 5月前

    Hey Allan,

    Sorry at the moment I can't as it's only available in a development environment. I think it will be released in 2 weeks... I'll check it out again once it's released and share it with you then.

    Sent from my iPhone

    On 6 Mar 2014, at 6:24 pm, "Allan Jardine" notifications.com wrote:

    Thanks :-)

    Are you able to link me to the page so I can tae a look at what is going on and ensure that I fix it correctly?

    — Reply to this email directly or view it on GitHub.

    点赞 评论 复制链接分享
  • weixin_39932611 weixin_39932611 5月前

    That would be great thanks. I'm just wondering why an undefined option would be added to the array. Or if the fix perhaps should be checking that the array has length ( elements.length !== 0 )?

    点赞 评论 复制链接分享
  • weixin_39702649 weixin_39702649 5月前

    Hey Allan,

    The project is now live, you can see it here: http://goo.gl/eFjoAX

    I'm not sure, either way could possibly work, as elements would still be defined.

    点赞 评论 复制链接分享
  • weixin_39932611 weixin_39932611 5月前

    Nice one - thanks for the link! Fixed now :-). I'm not 100% convinced about the event count (800+ events on that page?) but it is at least running now...

    点赞 评论 复制链接分享
  • weixin_39702649 weixin_39702649 5月前

    Thanks Allan :)!

    It may well be true lol... The form has like 5 steps with so many form elements and help tool tips and interactions. Each form field has at least 2-3 bindings too. Looks like when they started they didn't seem to like optimizing their code.

    Sent from my iPhone

    On 21 Mar 2014, at 12:33 am, "Allan Jardine" notifications.com wrote:

    Nice one - thanks for the link! Fixed now :-). I'm not 100% convinced about the event count (800+ events on that page?) but it is at least running now...

    — Reply to this email directly or view it on GitHub.

    点赞 评论 复制链接分享