0

Guys I have a javascript which has a each loop in which I am calling a particular function for each index value. This script executes on mouse scroll event upon reaching page end (lazy loading).

This is the main section of the script :

function (data) {
 $.each(data, function (index, value) {
   BindNotice(value);
     });
  }

Now the problem is, after the index reaches it max value, it restarts from the 0 index. So I am getting repeated data in my view. I want to restrict this script to only execute until the index has reached the max value. How would I go about doing this ?

EDIT : ADDED COMPLETE FUNCTION

function callMoreData()
    {
        $.ajax(
                    {
                        type: "GET",
                        url: "/api/values/getnotice",
                        dataType: "json",
                        crossDomain: true,
                        async: true,
                        cache: false,
                        allow: true,
                        success: function (data) {
                            $.each(data, function (index, value) {
                                    BindNotice(value);
                            });
                        },
                        error: function (x, e) {
                       alert('problem while fetching records!');
                        }
                    } );}

From JS file :

$(".mainwrap .innnerwrap").mCustomScrollbar({
    autoDraggerLength:true,
    autoHideScrollbar:true,
    scrollInertia:100,
    advanced:{
        updateOnBrowserResize: true,
        updateOnContentResize: true,
        autoScrollOnFocus: false
    },
     callbacks:{
            whileScrolling:function(){WhileScrolling();},
            onTotalScroll: function () {
            callMoreData();
        }

    }
});
7
  • Try this: if (index == data.length){return false;} Commented Jun 10, 2014 at 13:35
  • the loop wont start from 0 again, so your function(data) gets called again Commented Jun 10, 2014 at 13:36
  • 1
    It's more likely that it's the scroll event, it executes continously. The each loop only executes once, it doesn't restart by itself. Commented Jun 10, 2014 at 13:36
  • 1
    Can we see the code that triggers this function? Commented Jun 10, 2014 at 13:37
  • @SurrealDreams Likely a success from an Ajax call. Commented Jun 10, 2014 at 13:42

2 Answers 2

1

You could call another function, and then set it to do nothing after first go through.

function (data) {
    updateData(data);
}
function updateData(data) {
    updateData = function(data) {};
    $.each(data, function (index, value) {
        BindNotice(value);
    });
}

Demo using alerts.

I made some edits to the code because it sounds more like you're getting multiple events and they are writing over previous events. After the first call, the function is basically set to do nothing. Thus the first callback will be the only callback that calls BindNotice.

Edit

As I mentioned in the comments, to ever receive data again through this channel, you need to restore updateData. Here's an example:

function restoreUpdateData() {
    updateData = function(data) {
        updateData = function(data) {};
        $.each(data, function (index, value) {
            BindNotice(value);
        });
    };
}
Sign up to request clarification or add additional context in comments.

6 Comments

This keeps repeating the BindNotice function with the values from index 0.
@NewbieProgrammer, Yea, sorry I misunderstood your question. I made an edit which should only allow the first call to this function to execute (because it sounds like you're getting multiple calls to your function before the first finishes).
The edited script is executing perfectly. Thank you so much! much appreciated.
@NewbieProgrammer, Something that you need to be aware of is if you ever want to run this again you need to restore the updateData function.
what do you mean by restore the updateData function ?
|
0

Use a global variable to block other requests to this function.

Something like:

 var allow = true;
 function (data) {
     if(allow == true){
          allow = false;
          $.each(data, function (index, value) {
              BindNotice(value);
          });
          allow = true;
     }
 }

3 Comments

It's kinda rude to "placehold" your answer this way. Use a comment if you don't have a full answer ready yet. Just because SO uses points doesn't mean it's a competition.
I gave me answer "Use a global variable to block other requests to this function."
unfortunately this doesn't seem to work. No data is being loaded on scroll now.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.