I have a problem in passing an array to a function in JavaScript.
I have a Room with walls that has to be saved, but there are too many elements so i split them in parts. When u click on “save” this is the first function that is called:
function getCompleteParamlist(id)
{
var paramlist = $("#edit_form_"+id).serialize();
var params = paramlist.split("&");
var id = params[0];
var name = params[1];
var x_max = params[2];
var y_max = params[3];
var tools = params[4];
var style_toolbrush = params[5];
var roompiece_brushchooser = params[6];
params.splice(0,7);
var y = y_max.replace(/\D/g, '');
var length = params.length;
var fixed_params = id+"&"+name+"&"+x_max+"&"+y_max+"&"+tools+"&"+style_toolbrush+"&"+roompiece_brushchooser+"&y_val=";
prepareSaveRoom(fixed_params,params,y,length,1);
}
It gets all the values that are going to be saved, splits them into an array and builds an a string of params used every time. Removed the already used parameters and calls the next function:
function prepareSaveRoom(fixed_params,params,loops,length,count)
{
var temp_paramlist = fixed_params+count;
for(var i=1; i<=length/loops; i++)
{
temp_paramlist += "&"+params[0];
params.splice(0,1);
}
if (count == loops)
{
temp_paramlist += "&last=true";
saveRoom(temp_paramlist,1,fixed_params,params,loops,length,count);
}
else
{
temp_paramlist += "&last=false";
count++;
saveRoom(temp_paramlist,0,fixed_params,params,loops,length,count);
}
}
This works on the first run and the new temporary parameterlist is build and the saveRoom function is called. This builds a partial parameterlist that is going to be saved. removed the objects from the array and calls the save funvtion
function saveRoom(temp_paramlist,lastloop,fixed_params,params,loops,length,count)
{
alert(temp_paramlist);
$.ajax({
type: "POST",
dataType: "html",
url: "SaveRoom?"+temp_paramlist,
contentType: "application/x-www-form-urlencoded;charset=UTF-8",
cache: false,
success: function(data){
if(lastloop == 1){
location.href=data;
}
else{
window.setTimeout("prepareSaveRoom('"+fixed_params+"','"+params+"','"+loops+"','"+length+"','"+count+"')",2000);
}
},
error: function(data){
showErrorDialogHandleSecondDiv(data.responseText,"#edit_room");
}
});
}
This method calls a Java Controller which saves the values and then it calls the prepareSaveRoom function again, until all parameters are handled. But when saveRoom() calls prepareSaveRoom and it gets to the point:
for(var i=1; i<=length/loops; i++)
{
temp_paramlist += "&"+params[0];
params.splice(0,1);
}
I get the error that the Object has no method ‘splice’.
I can’t find the bug and so I’m trying to find some help here.
i don’t know why i can’t call splice on the parameter array that is passed from function to function.
Thanks for reading and possible help 🙂
You’re converting everything into a string. In fact, you’ve explicitly enclosed
paramswith quotes as if you wanted it to be a string:Strings do not have
.splice. You want to pass the arrayparams, not a string representation of it.Another reason not to pass a string to
setTimeout. You should pass a function at all times:That way, you indeed pass an array (so no string conversion).