How to create a JavaScript callback for knowing when an image is loaded?

.complete + callback This is a standards compliant method without extra dependencies, and waits no longer than necessary: var img = document.querySelector(‘img’) function loaded() { alert(‘loaded’) } if (img.complete) { loaded() } else { img.addEventListener(‘load’, loaded) img.addEventListener(‘error’, function() { alert(‘error’) }) } Source: http://www.html5rocks.com/en/tutorials/es6/promises/

What is a “callback” in C and how are they implemented?

There is no “callback” in C – not more than any other generic programming concept. They’re implemented using function pointers. Here’s an example: void populate_array(int *array, size_t arraySize, int (*getNextValue)(void)) { for (size_t i=0; i<arraySize; i++) array[i] = getNextValue(); } int getNextRandomValue(void) { return rand(); } int main(void) { int myarray[10]; populate_array(myarray, 10, getNextRandomValue); … … Read more

Callback to a Fragment from a DialogFragment

Activity involved is completely unaware of the DialogFragment. Fragment class: public class MyFragment extends Fragment { int mStackLevel = 0; public static final int DIALOG_FRAGMENT = 1; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); if (savedInstanceState != null) { mStackLevel = savedInstanceState.getInt(“level”); } } @Override public void onSaveInstanceState(Bundle outState) { super.onSaveInstanceState(outState); outState.putInt(“level”, mStackLevel); } void … Read more

Java executors: how to be notified, without blocking, when a task completes?

Define a callback interface to receive whatever parameters you want to pass along in the completion notification. Then invoke it at the end of the task. You could even write a general wrapper for Runnable tasks, and submit these to ExecutorService. Or, see below for a mechanism built into Java 8. class CallbackTask implements Runnable … Read more

Determine what attributes were changed in Rails after_save callback?

Rails 5.1+ Use saved_change_to_published?: class SomeModel < ActiveRecord::Base after_update :send_notification_after_change def send_notification_after_change Notification.send(…) if (saved_change_to_published? && self.published == true) end end Or if you prefer, saved_change_to_attribute?(:published). Rails 3–5.1 Warning This approach works through Rails 5.1 (but is deprecated in 5.1 and has breaking changes in 5.2). You can read about the change in this pull … Read more

Callback functions in Java

If you mean somthing like .NET anonymous delegate, I think Java’s anonymous class can be used as well. public class Main { public interface Visitor{ int doJob(int a, int b); } public static void main(String[] args) { Visitor adder = new Visitor(){ public int doJob(int a, int b) { return a + b; } }; … Read more

Defining TypeScript callback type

I just found something in the TypeScript language specification, it’s fairly easy. I was pretty close. the syntax is the following: public myCallback: (name: type) => returntype; In my example, it would be class CallbackTest { public myCallback: () => void; public doWork(): void { //doing some work… this.myCallback(); //calling callback } } As a … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)