Link to home
Start Free TrialLog in
Avatar of bemeall
bemeall

asked on

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?
Avatar of UnexplainedWays
UnexplainedWays

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


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;
}
 if(index < 1), my bad
ASKER CERTIFIED SOLUTION
Avatar of 0h4crying0utloud
0h4crying0utloud

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial