How to invoke a callback function with arguments - javascript

I have this main function which takes 1 string and 2 callback functions as the argument. Below is the code.
function confirmYesNo(confirmMessage, confirmCallback, cancelCallback) {
$("#yes-button").click(function () {
confirmCallback(); //How do I pass confirmCallback arguments
});
...
}
Here is how the function is invoked
function confirmYesNoTest() {
var confirmContent = "Some Message"
var id = "1";
confirmYesNo(confirmContent,
function (id) { alert(id); }, //alerts undefined
function () { alert("Cancel Clicked"); });
}
The problem is that the id variable above (which is set to 1) comes out as "undefined" when the confirmCallback executes. Seems something related to scope, but I am not able to get this working.

The callback should not take any arguments. You could capture the id in a closure:
function confirmYesNoTest() {
var confirmContent = "Some Message"
var id = "1";
confirmYesNo(
confirmContent,
function () { alert(id); },
function () { alert("Cancel Clicked"); }
);
}
Alternatively, if you didn't want to use closures you could pass the id as parameter to the callback:
function confirmYesNo(confirmMessage, id, confirmCallback, cancelCallback) {
$("#yes-button").click(function () {
confirmCallback(id);
});
...
}
and when invoking:
function confirmYesNoTest() {
var confirmContent = "Some Message"
var id = "1";
confirmYesNo(
confirmContent,
id,
function (id) { alert(id); },
function () { alert("Cancel Clicked"); }
);
}
Now you can use the callbacks as named functions:
var confirmCallback = function(id) {
alert(id);
};
and then:
function confirmYesNoTest() {
var confirmContent = "Some Message"
var id = "1";
confirmYesNo(
confirmContent,
id,
confirmCallback,
function () { alert("Cancel Clicked"); }
);
}

Related

Aframe pass variables between functions in component

I’m trying to stop the throttledFunction from running unless the “fly” event listener has been emitted. But I can’t change the “this.ballhit” variable from inside the eventlistener.
I don’t know how to pass variables between functions within the component.
AFRAME.registerComponent('ballflyact', {
init: function () {
var el = this.el;
this.ballhit = '';
el.addEventListener('fly', function () {
this.ballhit = true;
});
},
tick: function (t, dt) {
if (!this.ballhit) { return; }
this.throttledFunction(); // Called once a second.
},
});
When you create a function, this becomes different.
You can use self-binding function el.addEventListener('fly', () => { // ... });
Or like var self = this; el.addEventListener('fly', function () { self.ballHit = true; });
The following works. Thank you. Now the throttle function will only run for ~10,000 milliseconds after the “fly” event, not constantly in the background.
AFRAME.registerComponent('ballflyact', {
init: function () {
var el = this.el;
this.ballhit = '';
var self = this;
el.addEventListener('fly', function () {
self.ballhit = true;
// more code
setTimeout((e)=>{
self.ballhit = '';
}, 10000)
});
},
tick: function (t, dt) {
if (!this.ballhit) { return; }
this.throttledFunction(); // Called once a second.
},
});

How to call the sub function into the main function

Apps Script
function Docs() {
this.getActions = function(p) {
console.log("get action calling")
}
I need to call the getActions function in Docs funtion
function Docs() {
this.getActions = function(p) {
Logger.log("get action calling")
}
this.callActions = function () {
this.getActions();
}
}
function test() {
var docs;
docs = new Docs();
docs.getActions();
docs.callActions();
}

why can't i call one method from another inside an object

pardon my javascript ignorance: Why can't i do something like this in javascript? Running this tells me that theCalled is not defined. the order of the functions doesn't matter of course.
var myObj = {
theCaller: function() {
console.log('The Caller');
theCalled();
},
theCalled: function() {
console.log("i was called");
}
}
myObj.theCaller();
Add "this" before you call .theCalled()
var myObj = {
theCaller: function() {
alert('The Caller');
this.theCalled();
},
theCalled: function() {
alert("i was called");
}
}
myObj.theCaller();

Event listener in javascript

How to create a function that accepts the parameter as a function?
I have this function and I want to call it with parameter of a function also:
function ShowDialog(OnSubmit)
{
var _result = OnSubmit();
alert(_result);
}
This is what I want to call:
ShowDialog(
{
OnSubmit: function () {
return "Hello World";
}
}
);
this will alert "Hello World in browser"
Call:
ShowDialog(function(){
return "Hello World";
});
Then your function can remain as:
function ShowDialog(OnSubmit)
{
var _result = OnSubmit();
alert(_result);
}
JavaScript has first-class functions, so you can just pass the function reference directly, you do not need an object:
function showDialog(callback) {
var result = callback();
alert(result);
}
function bye() { return "bye!" };
showDialog(function() { return "Hello!" });
showDialog(bye);
var greetings = { ciao: function() { return "Ciao!" } };
showDialog(greetings.ciao);
Of course you can also pass the full object, in that case you need to modify the function as follow:
function showDialog(options) {
var result = options.onSubmit();
alert(result);
}
And then:
showDialog({
onSubmit: function() { return "Hello!" }
});
showDialog({
onSubmit: bye
});
You could also implement a function that accepts both object and callback:
function showDialog(options) {
var result = typeof options === 'function' ? options() : options.onSubmit();
alert(result);
}
showDialog({
onSubmit: bye
});
showDialog(bye);
Hope it helps.
Try this instead:
function ShowDialog(param)
{
var _result = param.OnSubmit();
alert(_result);
}
ShowDialog({
OnSubmit: function () {
return "Hello World";
}
});

System unique id of a function passed as a parameter (javascript)

This is probably not possible but maybe some of the stackoverflow geniuses can find a solution :)
W would like to have a function like this:
var myCrazyFunc;
myCrazyFunc = function (param1, callback) {
var funcId;
// I would like to get an Id of the function passed by callback
// which will be different for those two calls in example below
funcId = getFuncId(callback);
callback();
};
myCrazyFunc("param1", function () {
dosomething1;
});
myCrazyFunc("param1", function () {
dosomething2;
});
Please don't ask why I need that :) Simply it would simplify my code if that was possible.
Here is the function I made:
var myCrazyFunc;
var latestID = 0;
var funcToID = {};
function getFuncId(f) {
if (f in funcToID) {
return funcToID[f];
}
funcToID[f] = ++latestID;
return latestID;
}
myCrazyFunc = function(param1, callback) {
var funcId;
// I would like to get an Id of the function passed by callback
// which will be different for those two calls in example below
funcId = getFuncId(callback);
console.log(funcId);
callback();
};
myCrazyFunc("param1", function() {
'a';
});
myCrazyFunc("param1", function() {
'b';
});
this example would log:
1
2
I you run it with the same function code you get the same id, like here:
myCrazyFunc("param1", function() {
'a';
});
myCrazyFunc("param1", function() {
'a';
});
Ouput:
1
2
I hope that's ok.

Categories