[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1358
  • Last Modified:

undo/redo array javascript

I'm working on a solution to make a custom undo/redo function in javascript.
Everytime an action occurs, the content of a div is added to an array.
Undo gives back the content before the last action etc etc.

Now the problem is that I want to make it about 10 steps or something, because otherwise the browser uses too mutch memory.
But I can't just throw away the beginning items of the array.

Can someone give me some hints in the right direction on how to solve this problem?
0
bemeall
Asked:
bemeall
  • 4
2 Solutions
 
UnexplainedWaysCommented:
Maybe a cycling array?  I'm not to sure on the correct term.

(this would be done 1 @ a time, but just speeding it up.

[1][2][3][4][5][6][7][8][9][10]
^ index

You then add 3
[1][2][3][4][5][6][7][8][9][10]
              ^ index

You then add 5
[1][2][3][4][5][6][7][8][9][10]
                                   ^ index

Then you want and undo,
[1][2][3][4][5][6][7][8][9][10]
                               ^ index

Then..
[1][2][3][4][5][6][7][8][9][10]
                                   ^ index
....
[1][2][3][4][5][6][7][8][9][10]
                                        ^ index

Now here's the trick, you now loop back to the begining.
[1][2][3][4][5][6][7][8][9][10]
^ index
..
[1][2][3][4][5][6][7][8][9][10]
     ^ index
And you want an undo now
[1][2][3][4][5][6][7][8][9][10]
 ^ index

And another undo
[1][2][3][4][5][6][7][8][9][10]
                                        ^ index
And another undo

[1][2][3][4][5][6][7][8][9][10]
                                   ^ index


0
 
UnexplainedWaysCommented:
So as for code, you gonna need some if statments to catch all this,

like

undo()
{
  index--;
  if(index < 0)
      index = 10;
}

redo()
{
  index++;
  if(index < 10)
      index = 1;
}
0
 
UnexplainedWaysCommented:
 if(index < 1), my bad
0
 
0h4crying0utloudCommented:

i would use the array shift function (retrieve & removes elements from the front of the array) to remove elements

and unshift (adds to front) to add element unless the array is full in which case use pop(to get rid of the last element), then unshift.

Here is more info:

pop() Removes and returns the last element of an array
shift() Removes and returns the first element of an array
unshift() Adds one or more elements to the beginning of an array and returns the new length

http://www.w3schools.com/jsref/jsref_obj_array.asp

Good luck!
0
 
UnexplainedWaysCommented:
0h4crying0utloud's method would be alot better.  @ least i know for the future now.
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now