SitePoint Sponsor |
|
User Tag List
Results 1 to 2 of 2
-
Jun 12, 2007, 05:03 #1
assign value from function to new array?
I have a function which counts key strokes and I need to be able to access the values which it returns for further processing.
At the moment, the function itself works and the variable is correctly displayed in an alert box, but I don't seem to be able to assign that value to another var.
It seems I am missing something crucial here:
I have tried adding a return statement inside the function- not needed, and didn't help.
I tried declaring new variables inside the function and giving them the value totalKeyStrokes, with the result 'undefined'.
The array which I have just seems to be able to access the original value of totalKeySrokes (ie 0).
Is there anyone who can give me a clue?
This is an intermediate step which is holding me up....
here is the offending section of code:
/*counting key presses*/
var totalKeyStrokes = 0;
function CountKeyStrokes() {
if (foo % 2 == 0) {
totalKeyStrokes = ++totalKeyStrokes;
}
}
/*onkeyup used instead of onkeydown, to prevent multiple entries in windows if held down*/
document.onkeyup = CountKeyStrokes;
var incrementTotal = new Array(3);
incrementTotal[0] = totalKeyStrokes;
incrementTotal[1] = totalKeyStrokes;
incrementTotal[2] = totalKeyStrokes;
var incrementSet = (incrementTotal[0]+" "+incrementTotal[1]+" "+incrementTotal[2]);
-
Jun 16, 2007, 04:57 #2
ok i solved this shortly after posting, but was too busy to post here again. for anyone else who is stuck on the same thing, the answer is to do with global and local variables (I did think I had already tried that, but at 3am I get confused...)
Here is the finished, working thing (all finished now, too, this was a program for a psychology experiment, and it all works A-OK):
var totalKeyStrokes = 0;
incrementTotal = new Array(3);
incrementTotal[0] = 0
incrementTotal[1] = 0
incrementTotal[2] = 0
var incrementSet = (incrementTotal[0]+" "+incrementTotal[1]+" "+incrementTotal[2]);
function CountKeyStrokes() {
if (foo % 2 == 0) {
totalKeyStrokes = ++totalKeyStrokes;
if (foo == 2) {
incrementTotal[0] = totalKeyStrokes;
}
if (foo == 4) {
incrementTotal[1] = (totalKeyStrokes-incrementTotal[0]);
}
if (foo == 6) {
bar = (incrementTotal[0]+incrementTotal[1]);
incrementTotal[2] = (totalKeyStrokes-bar);
}
incrementSet = (incrementTotal[0]+" "+incrementTotal[1]+" "+incrementTotal[2]);
}
}
I'm quite glad no-one replied this time, I was worried they would think I was rude not checking back : )
Bookmarks