javascript - Calling a function in JS doesn't work with arguments -


in js file i'm calling 1 function other, @ first called no arguments using it's name handleresponse
tried adding arguments (of course changing function signature) , didn't anything, tried calling function handleresponse() , didn't work.
why can't call function using brackets or using arguments ?

here functions : main :

function sendrequest() {     var username = "";     var game_id = -1;     username = document.getelementbyid("username").value;     game_id = document.getelementbyid("game_id").value;     req.open('get', 'check_for_opponent.php?username='+username+'&game_id='+game_id);     req.onreadystatechange = handleresponse(username, game_id); <--- call     req.send(null); } 

calling : (i changed body, it's irrelevant).

function handleresponse(username, game_id) { if(req.readystate == 4) {     // something...     } } 

}

you need wrap call in anonymous function:

    req.onreadystatechange = function() {       handleresponse(username, game_id); <--- call     }; 

the code posted call handler @ point assignment made. instead of that, need assign function called when ready state changes.


Comments

Popular posts from this blog

php - get table cell data from and place a copy in another table -

javascript - Mootools wait with Fx.Morph start -

php - Navigate throught databse rows -