Register a SA Forums Account here!
JOINING THE SA FORUMS WILL REMOVE THIS BIG AD, THE ANNOYING UNDERLINED ADS, AND STUPID INTERSTITIAL ADS!!!

You can: log in, read the tech support FAQ, or request your lost password. This dumb message (and those ads) will appear on every screen until you register! Get rid of this crap by registering your own SA Forums Account and joining roughly 150,000 Goons, for the one-time price of $9.95! We charge money because it costs us money per month for bills, and since we don't believe in showing ads to our users, we try to make the money back through forum registrations.
 
  • Locked thread
Gazpacho
Jun 18, 2004

by Fluffdaddy
Slippery Tilde
it's a function that I write, but don't call?

why would I write it then?

Adbot
ADBOT LOVES YOU

pram
Jun 10, 2001
In computer programming, a callback is any executable code that is passed as an argument to other code, which is expected to call back (execute) the argument at a given time. This execution may be immediate as in a synchronous callback, or it might happen at a later time as in an asynchronous callback. In all cases, the intention is to specify a function or subroutine as an entity[clarification needed] that is, depending on the language, more or less similar to a variable.

Programming languages support callbacks in different ways, often implementing them with subroutines, lambda expressions, blocks, or function pointers.

Contents [hide]
1 Design
2 Implementation
3 Use
3.1 C
3.2 C#
3.3 JavaScript
3.4 Lua
3.5 Python
4 See also
5 References
6 External links
Design[edit]
There are two types of callbacks, differing in how they control data flow at runtime: blocking callbacks (also known as synchronous callbacks or just callbacks) and deferred callbacks (also known as asynchronous callbacks). While blocking callbacks are invoked before a function returns (in the C example below, which illustrates a blocking callback, it is function main), deferred callbacks may be invoked after a function returns. Deferred callbacks are often used in the context of I/O operations or event handling, and are called by interrupts or by a different thread in case of multiple threads. Due to their nature, blocking callbacks can work without interrupts or multiple threads, meaning that blocking callbacks are not commonly used for synchronization or delegating work to another thread.

Callbacks are used to program applications in windowing systems. In this case, the application supplies (a reference to) a specific custom callback function for the operating system to call, which then calls this application-specific function in response to events like mouse clicks or key presses. A major concern here is the management of privilege and security: whilst the function is called from the operating system, it should not run with the same privilege as the system. A solution to this problem is using rings of protection.

Implementation[edit]
The form of a callback varies among programming languages:

In assembly, C, C++, Pascal, Modula2 and similar languages, a machine-level pointer to a function may be passed as an argument to another (internal or external) function. This is supported by most compilers and provides the advantage of using different languages together without special wrapper libraries or classes. One example may be the Windows API that is directly (more or less) accessible by many different languages, compilers and assemblers.
C++ allows objects to provide their own implementation of the function call operation. The Standard Template Library accepts these objects (called functors), as well as function pointers, as parameters to various polymorphic algorithms.
Many interpreted languages, such as JavaScript, Lua, Python, Perl[1][2] and PHP, simply allow a function object to be passed through.
CLI languages such as C# and VB.NET provide a type-safe encapsulating reference, a "delegate", to define well-typed function pointers. These can be used as callbacks.
Events and event handlers, as used in .NET languages, provide generalized syntax for callbacks.
Functional languages generally support first-class functions, which can be passed as callbacks to other functions, stored as data or returned from functions.
Some languages, such as Algol 68, Perl, Python, Ruby, Smalltalk, C++11 and later, newer versions of C# and VB.NET as well as most functional languages, allow unnamed blocks of code (lambda expressions) to be supplied instead of references to functions defined elsewhere.
In some languages, e.g. Scheme, ML, JavaScript, Perl, Smalltalk, PHP (since 5.3.0),[3] C++11 and later, Java (since 8),[4] and many others, such functions can be closures, i.e. they can access and modify variables locally defined in the context in which the function was defined. Note that Java cannot, however, modify the local variables in the enclosing scope.
In object-oriented programming languages without function-valued arguments, such as in Java before its 8 version, callbacks can be simulated by passing an instance of an abstract class or interface, of which the receiver will call one or more methods, while the calling end provides a concrete implementation. Such objects are effectively a bundle of callbacks, plus the data they need to manipulate[clarification needed]. They are useful in implementing various design patterns such as Visitor, Observer, and Strategy.
Use[edit]
C[edit]
Callbacks have a wide variety of uses, for example in error signaling: a Unix program might not want to terminate immediately when it receives SIGTERM, so to make sure that its termination is handled properly, it would register the cleanup function as a callback. Callbacks may also be used to control whether a function acts or not: Xlib allows custom predicates to be specified to determine whether a program wishes to handle an event.

The following C code demonstrates the use of callbacks to display two numbers.

#include <stdio.h>
#include <stdlib.h>

/* The calling function takes a single callback as a parameter. */
void PrintTwoNumbers(int (*numberSource)(void)) {
int val1= numberSource();
int val2= numberSource();
printf("%d and %d\n", val1, val2);
}

/* A possible callback */
int overNineThousand(void) {
return (rand()%1000) + 9001;
}

/* Another possible callback. */
int meaningOfLife(void) {
return 42;
}

/* Here we call PrintTwoNumbers() with three different callbacks. */
int main(void) {
PrintTwoNumbers(&rand);
PrintTwoNumbers(&overNineThousand);
PrintTwoNumbers(&meaningOfLife);
return 0;
}
This should provide output similar to:

125185 and 89187225
9084 and 9441
42 and 42
Note how this is different from simply passing the output of the callback function to the calling function, PrintTwoNumbers() - rather than printing the same value twice, the PrintTwoNumbers calls the callback as many times as it requires. This is one of the two main advantages of callbacks.

The other advantage is that the calling function can pass whatever parameters it wishes to the called functions (not shown in the above example). This allows correct information hiding: the code that passes a callback to a calling function does not need to know the parameter values that will be passed to the function. If it only passed the return value, then the parameters would need to be exposed publicly.[examples needed]

Another example:

/*
* This is a simple C program to demonstrate the usage of callbacks
* The callback function is in the same file as the calling code.
* The callback function can later be put into external library like
* e.g. a shared object to increase flexibility.
*
*/

#include <stdio.h>
#include <string.h>

typedef struct _MyMsg {
int appId;
char msgbody[32];
} MyMsg;

void myfunc(MyMsg *msg)
{
if (strlen(msg->msgbody) > 0 )
printf("App Id = %d \nMsg = %s \n",msg->appId, msg->msgbody);
else
printf("App Id = %d \nMsg = No Msg\n",msg->appId);
}

/*
* Prototype declaration
*/
void (*callback)(MyMsg *);

int main(void)
{
MyMsg msg1;
msg1.appId = 100;
strcpy(msg1.msgbody, "This is a test\n");

/*
* Assign the address of the function "myfunc" to the function
* pointer "callback" (may be also written as "callback = &myfunc;")
*/
callback = myfunc;

/*
* Call the function (may be also written as "(*callback)(&msg1);")
*/
callback(&msg1);

return 0;
}
The output after compilation:

$ gcc cbtest.c
$ ./a.out
App Id = 100
Msg = This is a test
This information hiding means that callbacks can be used when communicating between processes or threads, or through serialised communications and tabular data. [clarification needed]

C#[edit]
A simple callback in C#:

public class Class1
{
static void Main(string[] args)
{
Class2 c2 = new Class2();

/*
* Calling method on Class2 with callback method as parameter
*/
c2.Method(CallBackMet);
}

/*
* The callback method. This method prints the string sent in the callback
*/
static void CallBackMet(string str)
{
Console.WriteLine("Callback was: " + str);
}
}

public class Class2
{
/*
* The method that calls back to the caller. Takes an action (method) as parameter
*/
public void Method(Action<string> callback)
{
/*
* Calls back to method CallBackMet in Class1 with the message specified
*/
callback("The message to send back");
}
}
JavaScript[edit]
Callbacks are used in the implementation of languages such as JavaScript, including support of JavaScript functions as callbacks through js-ctypes[5] and in components such as addEventListener.[6] However, a naive example of a callback can be written without any complex code:

function someAction(x, y, someCallback) {
return someCallback(x, y);
}

function calcProduct(x, y) {
return x*y;
}

function calcSum(x, y) {
return x + y;
}
// alerts 75, the product of 5 and 15
alert(someAction(5, 15, calcProduct));
// alerts 20, the sum of 5 and 15
alert(someAction(5, 15, calcSum));
First a function someAction is defined with a parameter intended for callback: someCallback. Then a function that can be used as a callback to someAction is defined, calcProduct. Other functions may be used for someCallback, like calcSum. In this example, someAction() is invoked twice, once with calcProduct as a callback and once with calcSum. The functions return the product and sum, respectively, and then the alert will display them to the screen.

In this primitive example, the use of a callback is primarily a demonstration of principle. One could simply call the callbacks as regular functions, calcProduct(x, y). Callbacks are generally useful when the function needs to perform actions before the callback is executed, or when the function does not (or cannot) have meaningful return values to act on, as is the case for Asynchronous JavaScript (based on timers) or XMLHttpRequest requests. Useful examples can be found in JavaScript libraries such as jQuery where the .each() method iterates over an array-like object, the first argument being a callback that is performed on each iteration.

Lua[edit]
A color tweening example using the ROBLOX engine that takes an optional .done callback:

wait(1)
local DT = wait()

function tween_color(object, finish_color, fade_time)
local step_r = finish_color.r - object.BackgroundColor3.r
local step_g = finish_color.g - object.BackgroundColor3.g
local step_b = finish_color.b - object.BackgroundColor3.b
local total_steps = 1/(DT*(1/fade_time))
local completed;
coroutine.wrap(function()
for i = 0, 1, DT*(1/fade_time) do
object.BackgroundColor3 = Color3.new (
object.BackgroundColor3.r + (step_r/total_steps),
object.BackgroundColor3.g + (step_g/total_steps),
object.BackgroundColor3.b + (step_b/total_steps)
)
wait()
end
if completed then
completed()
end
end)()
return {
done = function(callback)
completed = callback
end
}
end

tween_color(some_object, Color3.new(1, 0, 0), 1).done(function()
print "Color tweening finished!"
end)
Python[edit]
A classic use of callbacks in Python (and other languages) is to assign events to UI elements.

Here is a very trivial example of the use of a callback in Python. First define two functions, the callback and the calling code, then pass the callback function into the calling code.

>>> def my_callback(val):
... print("function my_callback was called with {0}".format(val))
...
>>> def caller(val, func):
... func(val)
...
>>> for i in range(5):
... caller(i, my_callback)
...
function my_callback was called with 0
function my_callback was called with 1
function my_callback was called with 2
function my_callback was called with 3
function my_callback was called with 4
See also[edit]
icon Computer programming portal
Command pattern
Continuation-passing style
Event loop
Event-driven programming
Implicit invocation
Inversion of control
libsigc++, a callback library for C++
Signals and slots
User exit

pram
Jun 10, 2001
hth op

big shtick energy
May 27, 2004


someone else calls you

i can understand how you'd be unfamiliar with this concept

Gazpacho
Jun 18, 2004

by Fluffdaddy
Slippery Tilde
i called the function but it's not working

There Will Be Penalty
May 18, 2002

Makes a great pet!
if your code isn't a spaghetti mess of callback functions everywhere then gently caress you

Gazpacho
Jun 18, 2004

by Fluffdaddy
Slippery Tilde
i'm a front end developer with 5 years of experiience, if this "callback" thing were actually useful i'd hvae heard of it by now

Gazpacho
Jun 18, 2004

by Fluffdaddy
Slippery Tilde
pls help, "myCallback" is the callback function i wrote and i'm calling it but it doesnt work the way its supposed to

code:
$(function() {
  setTimeout(myCallback(), 1000);
});

There Will Be Penalty
May 18, 2002

Makes a great pet!

Gazpacho posted:

pls help, "myCallback" is the callback function i wrote and i'm calling it but it doesnt work the way its supposed to

code:
$(function() {
  setTimeout(myCallback(), 1000);
});

You're calling myCallback within that function then passing its return value as the first argument of setTimeout. Get rid of the () after myCallback if you want to pass the myCallback function as the callback. lol at you if you say you have 5 years of <edit>frontend</edit> web development experience but never did this before.

There Will Be Penalty fucked around with this message at 05:37 on Jul 22, 2017

Gazpacho
Jun 18, 2004

by Fluffdaddy
Slippery Tilde
but then how do i call it

There Will Be Penalty
May 18, 2002

Makes a great pet!

Gazpacho posted:

but then how do i call it

setTimeout gets it invoked asynchronously after the specified timeout.

Gazpacho
Jun 18, 2004

by Fluffdaddy
Slippery Tilde
ok i put the call after the timeout like you said but it's still happening too soon

code:
$(function() {
  setTimeout(myCallback, 1000);
  myCallback();
});

Mr. Apollo
Nov 8, 2000

more like ballsack functions

echinopsis
Apr 13, 2004

by Fluffdaddy
lol just because she never calls you back doesnt mean its a function that doesnt happen to others

Video Nasty
Jun 17, 2003

dude the example is in that nice informative post by pram
code:
function someAction(x, y, someCallback) {

  return someCallback(x, y);

}
e: to be more clear - you need to define your callback as an argument in the parent function, then call it as a function in the return statement to call it and then pass on the arguments (if any) along.

There Will Be Penalty
May 18, 2002

Makes a great pet!

Gazpacho posted:

ok i put the call after the timeout like you said but it's still happening too soon

code:
$(function() {
  setTimeout(myCallback, 1000);
  myCallback();
});

setTimeout will call it after the timer expires. setTimeout() is not like a sleep() call. any statements after setTimeout() get executed immediately.

fixed:
code:
$(function() {
  setTimeout(myCallback, 1000);
});

skimothy milkerson
Nov 19, 2006


ill be the "printf("%d and %d\n", val1, val2);"

Gazpacho
Jun 18, 2004

by Fluffdaddy
Slippery Tilde
i took the call out and moved it inside the callback function so that setTimeout will call the function

code:
function myCallback() {
  myCallback();
  console.log("Ping!");
}

$(function() {
  setTimeout(myCallback, 1000);
});
now my page hangs! maybe there is a browser bug? (i am using firefox)

There Will Be Penalty
May 18, 2002

Makes a great pet!
wow

There Will Be Penalty
May 18, 2002

Makes a great pet!
:laffo:

There Will Be Penalty
May 18, 2002

Makes a great pet!
(you're calling the function from within itself which is causing the loop in case you're not fuckin with me)

Gazpacho
Jun 18, 2004

by Fluffdaddy
Slippery Tilde
pbbt i'm just gonna look for a JS library that spares me all this "callback" nonsense

NeoHentaiMaster
Jul 13, 2004
More well adjusted then you'd think.

There Will Be Penalty posted:

if your code isn't a spaghetti mess of callback functions everywhere then gently caress you

I promise it isn't.

pram
Jun 10, 2001

Mr. Apollo posted:

more like ballsack functions

:five:

OldAlias
Nov 2, 2013

pram posted:

In computer programming, a callback is any executable code that is passed as an argument to other code, which is expected to call back (execute) the argument at a given time. This execution may be immediate as in a synchronous callback, or it might happen at a later time as in an asynchronous callback. In all cases, the intention is to specify a function or subroutine as an entity[clarification needed] that is, depending on the language, more or less similar to a variable.

Programming languages support callbacks in different ways, often implementing them with subroutines, lambda expressions, blocks, or function pointers.

Contents [hide]
1 Design
2 Implementation
3 Use
3.1 C
3.2 C#
3.3 JavaScript
3.4 Lua
3.5 Python
4 See also
5 References
6 External links
Design[edit]
There are two types of callbacks, differing in how they control data flow at runtime: blocking callbacks (also known as synchronous callbacks or just callbacks) and deferred callbacks (also known as asynchronous callbacks). While blocking callbacks are invoked before a function returns (in the C example below, which illustrates a blocking callback, it is function main), deferred callbacks may be invoked after a function returns. Deferred callbacks are often used in the context of I/O operations or event handling, and are called by interrupts or by a different thread in case of multiple threads. Due to their nature, blocking callbacks can work without interrupts or multiple threads, meaning that blocking callbacks are not commonly used for synchronization or delegating work to another thread.

Callbacks are used to program applications in windowing systems. In this case, the application supplies (a reference to) a specific custom callback function for the operating system to call, which then calls this application-specific function in response to events like mouse clicks or key presses. A major concern here is the management of privilege and security: whilst the function is called from the operating system, it should not run with the same privilege as the system. A solution to this problem is using rings of protection.

Implementation[edit]
The form of a callback varies among programming languages:

In assembly, C, C++, Pascal, Modula2 and similar languages, a machine-level pointer to a function may be passed as an argument to another (internal or external) function. This is supported by most compilers and provides the advantage of using different languages together without special wrapper libraries or classes. One example may be the Windows API that is directly (more or less) accessible by many different languages, compilers and assemblers.
C++ allows objects to provide their own implementation of the function call operation. The Standard Template Library accepts these objects (called functors), as well as function pointers, as parameters to various polymorphic algorithms.
Many interpreted languages, such as JavaScript, Lua, Python, Perl[1][2] and PHP, simply allow a function object to be passed through.
CLI languages such as C# and VB.NET provide a type-safe encapsulating reference, a "delegate", to define well-typed function pointers. These can be used as callbacks.
Events and event handlers, as used in .NET languages, provide generalized syntax for callbacks.
Functional languages generally support first-class functions, which can be passed as callbacks to other functions, stored as data or returned from functions.
Some languages, such as Algol 68, Perl, Python, Ruby, Smalltalk, C++11 and later, newer versions of C# and VB.NET as well as most functional languages, allow unnamed blocks of code (lambda expressions) to be supplied instead of references to functions defined elsewhere.
In some languages, e.g. Scheme, ML, JavaScript, Perl, Smalltalk, PHP (since 5.3.0),[3] C++11 and later, Java (since 8),[4] and many others, such functions can be closures, i.e. they can access and modify variables locally defined in the context in which the function was defined. Note that Java cannot, however, modify the local variables in the enclosing scope.
In object-oriented programming languages without function-valued arguments, such as in Java before its 8 version, callbacks can be simulated by passing an instance of an abstract class or interface, of which the receiver will call one or more methods, while the calling end provides a concrete implementation. Such objects are effectively a bundle of callbacks, plus the data they need to manipulate[clarification needed]. They are useful in implementing various design patterns such as Visitor, Observer, and Strategy.
Use[edit]
C[edit]
Callbacks have a wide variety of uses, for example in error signaling: a Unix program might not want to terminate immediately when it receives SIGTERM, so to make sure that its termination is handled properly, it would register the cleanup function as a callback. Callbacks may also be used to control whether a function acts or not: Xlib allows custom predicates to be specified to determine whether a program wishes to handle an event.

The following C code demonstrates the use of callbacks to display two numbers.

#include <stdio.h>
#include <stdlib.h>

/* The calling function takes a single callback as a parameter. */
void PrintTwoNumbers(int (*numberSource)(void)) {
int val1= numberSource();
int val2= numberSource();
printf("%d and %d\n", val1, val2);
}

/* A possible callback */
int overNineThousand(void) {
return (rand()%1000) + 9001;
}

/* Another possible callback. */
int meaningOfLife(void) {
return 42;
}

/* Here we call PrintTwoNumbers() with three different callbacks. */
int main(void) {
PrintTwoNumbers(&rand);
PrintTwoNumbers(&overNineThousand);
PrintTwoNumbers(&meaningOfLife);
return 0;
}
This should provide output similar to:

125185 and 89187225
9084 and 9441
42 and 42
Note how this is different from simply passing the output of the callback function to the calling function, PrintTwoNumbers() - rather than printing the same value twice, the PrintTwoNumbers calls the callback as many times as it requires. This is one of the two main advantages of callbacks.

The other advantage is that the calling function can pass whatever parameters it wishes to the called functions (not shown in the above example). This allows correct information hiding: the code that passes a callback to a calling function does not need to know the parameter values that will be passed to the function. If it only passed the return value, then the parameters would need to be exposed publicly.[examples needed]

Another example:

/*
* This is a simple C program to demonstrate the usage of callbacks
* The callback function is in the same file as the calling code.
* The callback function can later be put into external library like
* e.g. a shared object to increase flexibility.
*
*/

#include <stdio.h>
#include <string.h>

typedef struct _MyMsg {
int appId;
char msgbody[32];
} MyMsg;

void myfunc(MyMsg *msg)
{
if (strlen(msg->msgbody) > 0 )
printf("App Id = %d \nMsg = %s \n",msg->appId, msg->msgbody);
else
printf("App Id = %d \nMsg = No Msg\n",msg->appId);
}

/*
* Prototype declaration
*/
void (*callback)(MyMsg *);

int main(void)
{
MyMsg msg1;
msg1.appId = 100;
strcpy(msg1.msgbody, "This is a test\n");

/*
* Assign the address of the function "myfunc" to the function
* pointer "callback" (may be also written as "callback = &myfunc;")
*/
callback = myfunc;

/*
* Call the function (may be also written as "(*callback)(&msg1);")
*/
callback(&msg1);

return 0;
}
The output after compilation:

$ gcc cbtest.c
$ ./a.out
App Id = 100
Msg = This is a test
This information hiding means that callbacks can be used when communicating between processes or threads, or through serialised communications and tabular data. [clarification needed]

C#[edit]
A simple callback in C#:

public class Class1
{
static void Main(string[] args)
{
Class2 c2 = new Class2();

/*
* Calling method on Class2 with callback method as parameter
*/
c2.Method(CallBackMet);
}

/*
* The callback method. This method prints the string sent in the callback
*/
static void CallBackMet(string str)
{
Console.WriteLine("Callback was: " + str);
}
}

public class Class2
{
/*
* The method that calls back to the caller. Takes an action (method) as parameter
*/
public void Method(Action<string> callback)
{
/*
* Calls back to method CallBackMet in Class1 with the message specified
*/
callback("The message to send back");
}
}
JavaScript[edit]
Callbacks are used in the implementation of languages such as JavaScript, including support of JavaScript functions as callbacks through js-ctypes[5] and in components such as addEventListener.[6] However, a naive example of a callback can be written without any complex code:

function someAction(x, y, someCallback) {
return someCallback(x, y);
}

function calcProduct(x, y) {
return x*y;
}

function calcSum(x, y) {
return x + y;
}
// alerts 75, the product of 5 and 15
alert(someAction(5, 15, calcProduct));
// alerts 20, the sum of 5 and 15
alert(someAction(5, 15, calcSum));
First a function someAction is defined with a parameter intended for callback: someCallback. Then a function that can be used as a callback to someAction is defined, calcProduct. Other functions may be used for someCallback, like calcSum. In this example, someAction() is invoked twice, once with calcProduct as a callback and once with calcSum. The functions return the product and sum, respectively, and then the alert will display them to the screen.

In this primitive example, the use of a callback is primarily a demonstration of principle. One could simply call the callbacks as regular functions, calcProduct(x, y). Callbacks are generally useful when the function needs to perform actions before the callback is executed, or when the function does not (or cannot) have meaningful return values to act on, as is the case for Asynchronous JavaScript (based on timers) or XMLHttpRequest requests. Useful examples can be found in JavaScript libraries such as jQuery where the .each() method iterates over an array-like object, the first argument being a callback that is performed on each iteration.

Lua[edit]
A color tweening example using the ROBLOX engine that takes an optional .done callback:

wait(1)
local DT = wait()

function tween_color(object, finish_color, fade_time)
local step_r = finish_color.r - object.BackgroundColor3.r
local step_g = finish_color.g - object.BackgroundColor3.g
local step_b = finish_color.b - object.BackgroundColor3.b
local total_steps = 1/(DT*(1/fade_time))
local completed;
coroutine.wrap(function()
for i = 0, 1, DT*(1/fade_time) do
object.BackgroundColor3 = Color3.new (
object.BackgroundColor3.r + (step_r/total_steps),
object.BackgroundColor3.g + (step_g/total_steps),
object.BackgroundColor3.b + (step_b/total_steps)
)
wait()
end
if completed then
completed()
end
end)()
return {
done = function(callback)
completed = callback
end
}
end

tween_color(some_object, Color3.new(1, 0, 0), 1).done(function()
print "Color tweening finished!"
end)
Python[edit]
A classic use of callbacks in Python (and other languages) is to assign events to UI elements.

Here is a very trivial example of the use of a callback in Python. First define two functions, the callback and the calling code, then pass the callback function into the calling code.

>>> def my_callback(val):
... print("function my_callback was called with {0}".format(val))
...
>>> def caller(val, func):
... func(val)
...
>>> for i in range(5):
... caller(i, my_callback)
...
function my_callback was called with 0
function my_callback was called with 1
function my_callback was called with 2
function my_callback was called with 3
function my_callback was called with 4
See also[edit]
icon Computer programming portal
Command pattern
Continuation-passing style
Event loop
Event-driven programming
Implicit invocation
Inversion of control
libsigc++, a callback library for C++
Signals and slots
User exit

ate shit on live tv
Feb 15, 2004

by Azathoth

Gazpacho posted:

i took the call out and moved it inside the callback function so that setTimeout will call the function

code:
function myCallback() {
  myCallback();
  console.log("Ping!");
}

$(function() {
  setTimeout(myCallback, 1000);
});
now my page hangs! maybe there is a browser bug? (i am using firefox)

5.

I've actually used a callback function in coffeescript for a hackathon at work (I'm not usually a coder). I don't think I quite grasped it, but it involved using a syscall to grab an NSLOOKUP then pass the results as a string. It worked and we won 3rd place out of 16 teams in the hackathon :3:

emoji
Jun 4, 2004

Gazpacho posted:

i took the call out and moved it inside the callback function so that setTimeout will call the function

code:

function myCallback() {
  myCallback();
  console.log("Ping!");
}

$(function() {
  setTimeout(myCallback, 1000);
});

now my page hangs! maybe there is a browser bug? (i am using firefox)

Leave b 4 u r expunged

duTrieux.
Oct 9, 2003

Gazpacho posted:

i took the call out and moved it inside the callback function so that setTimeout will call the function

code:
function myCallback() {
  myCallback();
  console.log("Ping!");
}

$(function() {
  setTimeout(myCallback, 1000);
});
now my page hangs! maybe there is a browser bug? (i am using firefox)

that's weird, you're code looks good. your cpu is probably failing if tha tmakes it hang

Gazpacho
Jun 18, 2004

by Fluffdaddy
Slippery Tilde
got it working

code:
$(function() {
  setTimeout(function() {
    myCallback();
  }, 1000);
});
callback's are dumb

echinopsis
Apr 13, 2004

by Fluffdaddy
mate

its you

crusader_complex
Jun 4, 2012
my god, its full of args

There Will Be Penalty
May 18, 2002

Makes a great pet!

crusader_complex posted:

my god, its full of args

:eyepop:

ConanTheLibrarian
Aug 13, 2004


dis buch is late
Fallen Rib

Gazpacho posted:

got it working

code:
$(function() {
  setTimeout(function() {
    myCallback();
  }, 1000);
});
callback's are dumb

yo man it would be cool if there was a framework that automatically wrapped your callbacks to make the code more readable

There Will Be Penalty
May 18, 2002

Makes a great pet!
lol if u can't understand this after glancing for five seconds

Maximum Leader
Dec 5, 2014
callbacks were invented by the devil

obstipator
Nov 8, 2009

by FactsAreUseless
lol at the op

akadajet
Sep 14, 2003

Gazpacho posted:

i'm a front end developer with 5 years of experiience

lol

Captain Foo
May 11, 2004

we vibin'
we slidin'
we breathin'
we dyin'

ban pram for account sharing with fishmech

The Management
Jan 2, 2010

sup, bitch?
as a millennial I only understand functional programming

Adbot
ADBOT LOVES YOU

power botton
Nov 2, 2011

programming serves no function

  • Locked thread