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?
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?
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;
}
like
undo()
{
index--;
if(index < 0)
index = 10;
}
redo()
{
index++;
if(index < 10)
index = 1;
}
if(index < 1), my bad
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
(this would be done 1 @ a time, but just speeding it up.
[1][2][3][4][5][6][7][8][9
^ index
You then add 3
[1][2][3][4][5][6][7][8][9
^ index
You then add 5
[1][2][3][4][5][6][7][8][9
^ index
Then you want and undo,
[1][2][3][4][5][6][7][8][9
^ index
Then..
[1][2][3][4][5][6][7][8][9
^ index
....
[1][2][3][4][5][6][7][8][9
^ index
Now here's the trick, you now loop back to the begining.
[1][2][3][4][5][6][7][8][9
^ index
..
[1][2][3][4][5][6][7][8][9
^ index
And you want an undo now
[1][2][3][4][5][6][7][8][9
^ index
And another undo
[1][2][3][4][5][6][7][8][9
^ index
And another undo
[1][2][3][4][5][6][7][8][9
^ index