All,
I'm working with this Ajax tutorial which basically pull some records from a database and display them. I want to modify the code with a button that will show the records one by one instead of together. The behavior I want to achieve is to get the next record each time the user clicks the Show next
button.
To do that I have built a little counter in the Ajax function that is used as an index to decide which array element to print. This doesn't work. **My question is: why doesn't my counter work?
Here is the html and Ajax code:
<html>
<body>
<script type="text/javascript">
<!--
function createRequest() {
//Returns HttpRequest
return request;
}
//My attempt at a counter. This doesn't work.
var index=null;
function calcIndex(){
if(index==null){
index=0;
}else{
index += index;
}
return index;
}
(.....)
</body>
</html>
Your calcIndex
function declaration is broken, it's missing the function
part. And are you sure you want to set index += index
? That would be sort of odd. Not only that, even if you fix it and leave it as-is, index will never increment past zero:
var index=null;
function calcIndex(){
if(index==null){
index=0; // First call, now index = 0
}else{
index += index; // Second, third, ... nth call: index = 0 + 0
}
return index;
}
Let's simplify:
var index = 0;
function calcIndex(){
return index++; // Returns zero first, then 1, then 2...
}
But wait, at that point why do you need a function at all? Instead you can simply do:
var index = 0;
...
//index = calcIndex(); No point here
var queryString = "?age=" + age + "&wpm=" + wpm + "&sex=" + sex + "&index=" + index++;
^^^^^^^^
Cheers