Jump to content

Do I need a Call Back Function?


unemployment

Recommended Posts

I'm running... but console.log(ajax_call(info)); keeps being displayed as undefined.

 

function autosuggest_results (info){				
if (info.length > 1)
{		
	var with_field		= document.getElementById('users');
	var with_auto_id	= document.getElementById('with_auto');
	var search_emp_id	= document.getElementById('search_employees');
	var users_list		= document.getElementsByName('with_uid[]');

	if(with_auto_id){
		var with_auto		= with_auto_id;
	}else{
		var with_auto		= search_emp_id;
	}

	ajax_call(info);

	console.log(ajax_call(info));

}
else
{
	hide();
}
}

function ajax_call(info){
var with_u_list = prev_picked();

ajax.get('/assets/ajax/associates_search.php?info=' + info + '&sid=' + hex_md5(sess_id()) + '&list=' + with_u_list, function(resp)
{		
	var search 	= eval('(' + resp + ')');

	show();

	var with_auto_id	= document.getElementById('with_auto');
	var search_emp_id	= document.getElementById('search_employees');

	if(with_auto_id){
		var with_auto		= with_auto_id;
	}else{
		var with_auto		= search_emp_id;
	}

	if(search.length > 0){
		fill_list(search);
	}else{
		removeChildren(with_auto);

		var no_res_div = document.createElement('div');

		no_res_div.className = 'as_no_res';

		appendText(no_res_div, 'No associates found for \'' + info + '\'. He/she may already have been added.');
		with_auto.appendChild(no_res_div);
	}

	//console.log(search);
	return search;
});
}

 

 

Link to comment
https://forums.phpfreaks.com/topic/240767-do-i-need-a-call-back-function/
Share on other sites

You could do that. This is how it works the ajax request is done and starts waiting for a response. However the javascript is being run directly after the request and it's not waiting for the response to continue.

 

You can either use a callback function to run within the ananymous function inside get() or you could do synchronous(by setting async:false) call which locks up the whole javascript execution untill you get a response.

 

I'd go for the callback.

You could do that. This is how it works the ajax request is done and starts waiting for a response. However the javascript is being run directly after the request and it's not waiting for the response to continue.

 

You can either use a callback function to run within the ananymous function inside get() or you could do synchronous(by setting async:false) call which locks up the whole javascript execution untill you get a response.

 

I'd go for the callback.

 

Can you please show me how to do this?  I have never made a call back function and reading about them has just confused me.  It would be great if you could give me an example.

It looks like you're already using a call back to me..?

 

ajax.get('/assets/ajax/associates_search.php?info=' + info + '&sid=' + hex_md5(sess_id()) + '&list=' + with_u_list, function(resp)

{

              // this is your call back code...

}

Have you tried this?

ajax.get('/assets/ajax/associates_search.php?info=' + info + '&sid=' + hex_md5(sess_id()) + '&list=' + with_u_list, function(resp)
{
    console.log(resp); 
}

 

Yes, that of course works.  I don't think you're really understanding my problem.

 

I have these two functions.

 

function associate_search (){
    this.autosuggest_results	= autosuggest_results;
        
    function autosuggest_results (info){				
if (info.length > 1)
{		
	var with_field		= document.getElementById('users');
	var with_auto_id	= document.getElementById('with_auto');
	var search_emp_id	= document.getElementById('search_employees');
	var users_list		= document.getElementsByName('with_uid[]');

	if(with_auto_id){
		var with_auto		= with_auto_id;
	}else{
		var with_auto		= search_emp_id;
	}

	var with_u_list = prev_picked();

	ajax.get('/assets/ajax/associates_search.php?info=' + info + '&sid=' + hex_md5(sess_id()) + '&list=' + with_u_list, function(resp)
	{		
		var search 	= eval('(' + resp + ')');

		show();

		var with_auto_id	= document.getElementById('with_auto');
		var search_emp_id	= document.getElementById('search_employees');

		if(with_auto_id){
			var with_auto		= with_auto_id;
		}else{
			var with_auto		= search_emp_id;
		}

		if(search.length > 0){
			fill_list(search);
		}else{
			removeChildren(with_auto);

			var no_res_div = document.createElement('div');

			no_res_div.className = 'as_no_res';

			appendText(no_res_div, 'No associates found for \'' + info + '\'. He/she may already have been added.');
			with_auto.appendChild(no_res_div);
		}

		return search;
	});

}
else
{
	hide();
}
     }
}

with_field.onkeyup = function(event)
{
var ee = associate_search.autosuggest_results (with_field.value);
console.log(ee);
}

 

I keep getting undefined in my console.log(ee) even though search is being returned in the ajax callback.

I did explain why that doesn't work in the first post and how to make that work. If you want the function to work that way you will need to a synchronous, rather than

asynchronous call. But I can't show you how to do that because the function ajax.get isn't shown in your post.

 

The thing that's unclear is why you want the value to be returned? What do you need to do with that data that's retrieved using an ajax call?

 

I did explain why that doesn't work in the first post and how to make that work. If you want the function to work that way you will need to a synchronous, rather than

asynchronous call. But I can't show you how to do that because the function ajax.get isn't shown in your post.

 

The thing that's unclear is why you want the value to be returned? What do you need to do with that data that's retrieved using an ajax call?

 

I made my ajax synchronous and that still made my console.log() show up undefined.  Any other ideas?

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.