Pass correct “this” context to setTimeout callback?

EDIT: In summary, back in 2010 when this question was asked the most common way to solve this problem was to save a reference to the context where the setTimeout function call is made, because setTimeout executes the function with this pointing to the global object: var that = this; if (this.options.destroyOnHide) { setTimeout(function(){ that.tip.destroy() … Read more

How can I pass a parameter to a setTimeout() callback?

setTimeout(function() { postinsql(topicId); }, 4000) You need to feed an anonymous function as a parameter instead of a string, the latter method shouldn’t even work per the ECMAScript specification but browsers are just lenient. This is the proper solution, don’t ever rely on passing a string as a ‘function’ when using setTimeout() or setInterval(), it’s … Read more

How do I create delegates in Objective-C?

An Objective-C delegate is an object that has been assigned to the delegate property another object. To create one, you define a class that implements the delegate methods you’re interested in, and mark that class as implementing the delegate protocol. For example, suppose you have a UIWebView. If you’d like to implement its delegate’s webViewDidStartLoad: … Read more

How to return value from an asynchronous callback function? [duplicate]

This is impossible as you cannot return from an asynchronous call inside a synchronous method. In this case you need to pass a callback to foo that will receive the return value function foo(address, fn){ geocoder.geocode( { ‘address’: address}, function(results, status) { fn(results[0].geometry.location); }); } foo(“address”, function(location){ alert(location); // this is where you get the … Read more

How do I convert an existing callback API to promises?

Promises have state, they start as pending and can settle to: fulfilled meaning that the computation completed successfully. rejected meaning that the computation failed. Promise returning functions should never throw, they should return rejections instead. Throwing from a promise returning function will force you to use both a } catch { and a .catch. People … Read more

What is the use of PHP Callbacks? [closed]

There’s little use in PHP for callbacks which will be called when an action has completed, e.g.: string(‘Ahmar’, function ($name) { echo … }) This is very idiomatic in languages like Javascript; but that is because Javascript has a rather different mode of execution than PHP does. In Javascript many things are usually happening at … Read more