Extract array values with last extraction memory on callback in javascript -


Assume that I have a dynamic array that is contained within many different strings of population on page loads: "string1", "string2", "string3", "string4", "string5", "string6", "string7", "string8", "string9", "string10", "string11 "" String12 "," string13 "," string14 "," string15 "," string16 "," string17 "," string18 "];

I have a function that is called every event (we call a click) which is required to get 3 strings of the array continuously, while remembering that At which price it was left, last time it was called. Therefore:

The first time the function is called, it gives returns:

  string1, string2, string3   

It is called, it gives returns:

  string4, string5, string6   

and so on ...

code me Not required, instead of the click event or callback function, a code for the function that will generate extraction every time and bring it back. Something simple like being able to call:

  arr.runExtraction ();   

on each callback and to bring it back to the desired data.

What if the array ends? Start from the beginning?

You can do something like this:

  function get_iterator (value, step, start) {steps = steps} 1; Var start = start || 0, max = values.length; Return function () {var end = current + steps, end = end> Maximum? Maximum: end, t = value Sali (current, end); Current = end% max; // or if you do not want to wrap around: // current = end; Return T; }}   

Edit: Added Start parameter. As current as values.length ,

splice The empty table will return if you do not wrap around. Slice will not change the original array using

.

and then

  var extract = get_iterator (arr, 3); Var arr1 = Remove (); // gives you the first three elements var arr2 = extract; // gives you the next three elements.   

If the number of elements is not divisible by number, then this can give you less element in "final" extraction, the next call from the extracted elements Will start You can also modify it that it wraps around and takes the element from the beginning so that it can always return as many elements you specify.

Context :

Comments