0

I have a fairly basic inquiry regarding how to stop an .each() loop after the conditions have been met. I have a notification counter that displays the number of Issues within a given timeframe. It works, but if I were select another option, it will add up the notifications, which is not good. Below is an example of Issues within 24 hours. I also have an option for Issues within one week and 30 days.

For example, if I were to select 24 hours, it will show: 2

If I were then to select One Week, it will 12 (One Week = 10)

I've tried 'return false' at the end of the loop, but no luck.

Let me know if you can help me out. Thank you.

// # of Issues within 24 hours
            var newIssues, openIssues, closedIssues;
            newIssues = 0;
            openIssues = 0;
            closedIssues = 0;

            $('.newIssueCount').text('-');
            $('.openIssueCount').text('-');
            $('.closedIssueCount').text('-');

            $('ul.timeFrameRange li:nth-child(1)').click( function(e) {
                e.preventDefault();             
                $.each(parsedData, function (index, item) {
                if (moment(item.CreatedDate).isAfter(twentyFourHours)) {
                        newIssues++;
                        $('.newIssueCount').html(newIssues);

                        if (item.IsClosed == false) {
                            openIssues++;
                            $('.openIssueCount').html(openIssues);
                        }
                        else if (item.IsClosed == true) {
                            closedIssues++;
                            $('.closedIssueCount').html(closedIssues);
                        }
                        else if (newIssues == 0 || openIssues == 0 || closedIssues == 0) {
                            $('.newIssueCount').html('0');
                            $('.openIssueCount').html('0');
                            $('.closedIssueCount').html('0');
                        }
                    }

                    return false;
                });
            });
  • 1
    return false; should do the trick. I don't see it in your code, can you paste it? – Pitto Jan 02 '20 at 20:10
  • Does that condition ever evaluate as true? Did you try to put a console.log before the return false to see if it ever gets there? – Pitto Jan 02 '20 at 20:14
  • @Pitto it does evaluate as true as the counter adds up all the Issues when the conditions are met. –  Jan 02 '20 at 20:18

1 Answers1

0

Please try to return false from an anonymous function:

  if (your_condition) return false;
Pitto
  • 8,229
  • 3
  • 42
  • 51