Sherwin Flight
Sherwin Flight

Reputation: 2363

Passing variables in a JavaScript callback

I'm having trouble finding some information, and it's almost certainly because I don't know the correct terminology for what I'm doing. When I search for info about variables in a callback function, the code not the same as what I am trying to do here.

I have some JavaScript code, this is part of it:

var myNotification = new Notify('Notification Title', {
    body: 'message goes here',
    icon: "/icon.png",
    tag: 'for app use',
    notifyClick: functionNameHere,
    timeout: 10
});

The "functionNameHere" part is the name of another function, that is called when the notification created by this script is clicked.

I need to be able to pass a variable along with it, so essentially what i need to do is:

var myNotification = new Notify('Notification Title', {
    body: 'message goes here',
    icon: "/icon.png",
    tag: 'for app use',
    notifyClick: functionNameHere('variableContentWouldbeHere'),
    timeout: 10
});

However, when I do it like that it doesn't work properly.

How would I achieve this?

Upvotes: 1

Views: 75

Answers (5)

Sohel
Sohel

Reputation: 81

I am not totally sure what you are trying to do, but I guess may be you need something like this:

var myVariable = {};
var myNotification = new Notify('Notification Title', {
   body: 'message goes here',
   icon: "/icon.png",
   tag: 'for app use',
   notifyClick: function(e){
      functionNameHere(myVariable);
   },
   timeout: 10
});

Upvotes: 1

Mamdouh Saeed
Mamdouh Saeed

Reputation: 2324

var myNotification = new Notify('Notification Title', {
    body: 'message goes here',
    icon: "/icon.png",
    tag: 'for app use',
    notifyClick: function(){
    functionNameHere('variableContentWouldbeHere');    
    },
    timeout: 10
});

Upvotes: 1

VadimB
VadimB

Reputation: 5711

You can achive this with changing function scope or using closures.

1st:

notifyClick: functionNameHere.bind('variableContentWouldbeHere');

And your context inside "functionNameHere" implementation is your argument.

function functionNameHere() {
   console.log(this === 'variableContentWouldbeHere'); //true
}

2nd:

Using closures

notifyClick: function() { functionNameHere('variableContentWouldbeHere'); }

Upvotes: 2

Stefan Negele
Stefan Negele

Reputation: 1112

You could pass an anonymous function which calls your function like this:

var myNotification = new Notify('Notification Title', {
    body: 'message goes here',
    icon: "/icon.png",
    tag: 'for app use',
    notifyClick: () => functionNameHere('variableContentWouldbeHere'),
    timeout: 10
});

Upvotes: 0

Anders Bornholm
Anders Bornholm

Reputation: 1336

You can't affect how the callback is invoked, the code in the Notify class determines that. If you try to pass it like in your second example, the function "functionNameHere" will be called immediately and the result of it is passed to the Notify constructor as the callback function - which is not what you want.

What you could do is make your functionNameHere function be a wrapper that returns another function that will be used as the callback.

Upvotes: 0

Related Questions