Forum Moderators: open

Message Too Old, No Replies

consective function calls creating problem

         

phparion

12:13 pm on Aug 24, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



hi

please see the following code,

Code:

for (var i=0; i<spn.length; i++) {
f[i].value = res[i+1];
checkHistory(res[i+1])
//alert (res[i+1])
spn[i].innerHTML = res[i+1];
}


this loop runs four times for four keywords and the checkHistory() reads PHP session array and searches if any keyword is already present in the SESSION. If present then it reads its relative suggestion from the mySQL DB and displays it.

now the problem is that if the visitor has seen suggestion for fourth keyword that is the last keyword of the loop then it displays the suggestion in the DIV properly but if the user has seen suggestion for any keyword except fourth that is the last one then it doesnt show it.

if i comment ALERT it does not show it, BUT if i un-comment ALERT it does display it. I am lost that what it has to do with ALERT.. can you shed some light on this please?

thanks

orion_rus

5:51 am on Aug 25, 2006 (gmt 0)

10+ Year Member



type more about your variables ,res ,f and spn, what is it?

phparion

6:34 am on Aug 25, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



in fact it is an AJAX application that fetches data from the mySQL DB and display it.

'res' is an array that i get back from AJAX function response.

spn is an array of radio buttons. the four keywords have on radio button in front of each that radio button value is assigned in this loop. as i fetch keywords from DB so i assign them to radio buttons one by one in the loop at the same time i call a function checkHistory() to see if any of keyword is present in current session of the user if it is it means user has seen its response before so i will automatically display its response too so that user can see what keywords' response he has seen before in the current session.

i hope it makes more sense now..

daveVk

7:52 am on Aug 25, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Assuming checkHistory initiates ajax requist, you may need to wait for reply before calling it again?

phparion

10:17 am on Aug 25, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



checkHistory(current keyword of loop) calls a php page that checks if the current keyword is present in the session array. if yes then it means user has already seen its response so it will return YES word. if YES is returned checkHistory() body calls another function called ShowRec() that function calls another PHP page and sends the keyword to fetch response for this keyword from the database and then assign it to the <div id="response"></div>

the wierd problem is that if i place the ALERT before checkHistory() function then it workes in the desired way. But if i remove ALERT from there then it doesn't call the ShowRec function even it founds the keyword in the session array...

i am just lost that what alert has to do with it and how can i fix it?

pls help i am just lost..

daveVk

11:53 am on Aug 25, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



All the steps you describe take time. With alert in place there is time to complete these steps prior to checkHistory being recalled. Your code assumes checkHistory does not return until response from server is recieved, it more likely returns after sending request only.

phparion

12:22 pm on Aug 25, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



yes, i agree with you, bcz i placed an ALERT in checkHistory()to confrim its call.

when i place ALERT in my previous shown code then it does also show the ALERT of checkHistory() body but if i comment ALERT in my previously shown code it also doesn't call checkHistory() body ALERT, it means that code does found keyword in the session but at the same time it doesn't call checkHistory()...

how can i solve this?

daveVk

1:19 pm on Aug 25, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Change above code to only call do "f[i]= " and "checkHistory" for first item. Find place where requist reply recieved and do this bit "spn[i].innerHTML = res[i+1];" increment counter "i" ( make it global ) and if not all done call do "f[i]= " and "checkHistory" again.