alexnofue Posted October 10, 2008 Share Posted October 10, 2008 Hi, i've been 'chaining' functions with eval, it seems to work okay except for one detail, i'm not so sure if the first function it's on hold waiting for a second to finish.for example function one(){ ... eval('two()'); ...}function two(){ ... alert('hello');} my problem with this is that the two() migth have holding one() for as long as the user closes the alert, so it's not nice if the first one is an ajax or something.or it's there a workaround for this? Link to comment Share on other sites More sharing options...
justsomeguy Posted October 10, 2008 Share Posted October 10, 2008 Javascript function calls are synchronous, so the function will wait for the other one to return before moving on. Why are you using eval though, why not just call the function the normal way? Link to comment Share on other sites More sharing options...
alexnofue Posted October 10, 2008 Author Share Posted October 10, 2008 Hi, thanks JSG for your reply, if it works that way i better be very careful with the 'chaining', i'm doing this because i'm sending the name of the function as a parameter to a function that pick it and put it in the eval.To be more specific i put together a function for the ajax requests that i might need so i call it every time i need ajax, so it works similar to this:function goajaxgo(url, param, ... , 'function_name()'){ ... ... ... //when the request is finished eval('function_name()');}so i'm sending a lot of function names to the same function, if you want i could post the whole code but i don't consider it adds much.greetings. Link to comment Share on other sites More sharing options...
justsomeguy Posted October 10, 2008 Share Posted October 10, 2008 You can also just send the function itself, instead of the name. function func1(){ alert("func1"); func2(func3);}function func2(fn){ alert("func2"); if ((typeof fn) == "function") fn();}function func3(){ alert("func3");} Link to comment Share on other sites More sharing options...
jeffman Posted October 10, 2008 Share Posted October 10, 2008 I use JSG's technique when posting AJAX data. It looks a little like this: function post_data (data, callback, mode) { // stuff AJAX.onreadystatechange = callback; // stuff} It makes the function very reusable, which I guess is what you are after. Link to comment Share on other sites More sharing options...
alexnofue Posted October 10, 2008 Author Share Posted October 10, 2008 Hi, well i change the eval for the JSG's code suggestion and it worked just as i expected to, meaning that after the change everything keep working plus the new code makes sure it's a function.I'll try to keep this 'chaining' to the minimun and the chained functions short and simple.thanks for your replies JSG and DD. Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.