Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I need to create a function with a callback function, but the solutions I've found does not allow to set the function.
I'll explain:
this is the solution I've found:
function callbackFunction()
{
alert("hello world");
}
function myFunction(callback)
{
callback()
}
myFunction(callbackFunction()) /* this works */
this is what i need:
function myFunction(callback)
{
callback()
}
myFunction(function(){alert("hello world");}); /* this doesn't work */
Some ideas?
Thank you
myFunction(callbackFunction()) /* this works */
No it doesn't. At least not in the way you think it does. This is:
Invoking callbackFunction
Passing the result of callbackFunction to myFunction
You're probably getting an error from within myFunction when it tries to invoke callback since that's not a function. But you're ignoring that because you see the alert() and think it works. The alert() happened before myFunction was invoked.
You want to pass it as a function reference, not a function invocation:
myFunction(callbackFunction) /* this works */
This would produce the same visible result (the alert()) but in the expected order of operations and without the error.
myFunction(function(){alert("hello world");}); /* this doesn't work */
You sure about that? If that is indeed "not working" for you then there must be more to the problem that you're not sharing with us, because that code works as-is.
Related
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
I have two .js files. On the first .js file i declare and execute a function like this:
(function($){
function something(){
//code here
}
$(document).ready(function(){
something();
});
})(jQuery);
Also, I want to execute the something() function in the second .js. My code is the following:
(function($){
$(document).ready(function(){
//more code
something();
//more code
});
})(jQuery);
However, when the program is executed, I get the following error:
Uncaught ReferenceError: something is not defined(…)
Possible Solution:
I solved the problem declaring the function as:
(function($){
this.something = function(){
//code here
}
})(jQuery);
Is this 100% right?
You cant define a function inside a scope and try to use that function in another scope. You have not understood how functions and scopes work in JavaScript.
Have a look at this.
It's normal, you declared your function something in an IIFE, so you can't using it outside your block. Try to declare your function outside (function($) ...)() block.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
I found this setTimeout() method example on W3 Schools and noticed something that I cannot explain. The example is:
myVar = setTimeout(alertFunc, 3000);
...which works fine. But when I change it to
myVar = setTimeout(alertFunc(), 3000);
...the alert triggers instantly. Why? Shouldn't it be the same?
shouldn´t it be the same?
No, not at all.
setTimeout(alertFunc, 3000) passes the value of alertFunc (a reference to a function) into setTimeout. setTimeout stores that function reference in order to call it three seconds later.
setTimeout(alertFunc(), 3000) calls alertFunc, immediately, and passes its return value into setTimeout. Exactly the way foo(bar()) calls bar and passes its return value into foo.
The setTimeout() accepts a function as a first parameter and the time as the second parameter. I hope you have heard of functions returning a function as well. So for that cases, you can also call the function there.
The moment you add () to any function, it calls it immediately. So you call the function and return nothing to execute to the setTimeout.
Case 1
setTimeout(myFunc, 3000);
Here you are passing the function itself to get executed after 3 seconds.
Case 2
setTimeout(myFunc(), 3000);
Here you are passing the function's executed return value to get executed after 3 seconds.
myVar = setTimeout(alertFunc(), 3000); is the same as:
aF = alertFunc(); myVar = setTimeout(aF, 3000);
From this you can see that alertFunc is called outside of setTimeout.
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I am getting the error:
bluebird.js:2118 Uncaught TypeError: generatorFunction must be a function
But I don't understand why this error is occurring, as I am passing it a generatorFunction.
export class Welcome {
fetch() {
Promise.coroutine(this.userData());
}
userData = function* getData() {
this.lotsData = yield this.testApi.getMock();
this.lotsData = JSON.stringify(this.lotsData, null, 4);
}
}
So a click event calls fetch() and that calls this.userData(). This is the console dump of this.userData:
GeneratorFunctionPrototype {}
_invoke: invoke(method, arg)
__proto__: GeneratorFunctionPrototype
Which tells me it most certainly is a generator. I am using all of this in an aurelia class if that somehow makes any difference (which it shouldn't I don't think). So why is it erroring that the generatorFuction must be a function?
The limited amount of code you posted is riddled with errors, but there is not enough to know what is correct and what is incorrect.
One thing is for sure this.userData() is a function call and not a reference to a function, which is what .coroutine() is expecting.
Whatever type returned by this.testApi.getMock() is what is being yielded, which we have no idea given what you posted, but that is probably not even relevant at this point, because what is returned is probably unknown/null anyway because you are assigning the yield to a variable of questionable scope. Your code is nowhere near the example from the documentation
PingPong.prototype.ping = Promise.coroutine(function* (val) {
console.log("Ping?", val)
yield Promise.delay(500)
this.pong(val+1)
});
The documentation has a very clear and well defined example you should follow, explicitly until you understand what you are actually doing.
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I'm trying to figure out how to access a variable inside a .when.done function. here's the example:
var colviews = {
1: true,
2: true,
3: false
}
$.when( // run only -when- these script get loaded
$.getScript( "/mckinney_images/jquery.tablesorter.all.js" )).done(function(){
$(function(){
console.log(colviews); // How do I get the colviews variable here?
});
});
I have a basic understanding of scope but not sure how it applies inside a when function.
I have a basic understanding of scope
Good! So you know what scope is, how closures work and how var works.
…but not sure how it applies inside a when function.
Simple: It's no different! Yes, the done callback function is invoked asynchronously, but that doesn't change how scope works.
You just are getting the colviews variable there successfully (if it doesn't log the expected value, that means it was overwritten elsewhere or the callback is never called because $.getScript failed).
$.when does no magic (and in fact, in your example it's not needed at all, your code works the same when you omit it).
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 9 years ago.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Improve this question
I have two files:
File A:
var A = (function () {
return {
bindClick: function (fun) {
$('#btnId').on('click', fun);
}
};
}());
File B:
(function ($) {
$(document).ready(function () {
var doSomething = function (what) {
console.log(what);
}
A.bindClick(doSomething(1));
});
})(jQuery);
This print me "binded" only once and when I click button nothing happen..
What is the right way to bind a function passing as parameter?
Problem is that I called:
A.bindClick(doSomething(1));
But I was wrong the behavior of the function: it had to return a function, in this way:
var doSomething = function(what) {
return function() {
console.log(what);
}
}
I hope it will be useful to someone!
Besides being somewhat convoluted way of doing things, it seems like it should work. And indeed it does work in the jsfiddles that the commenters have linked to...
I'd guess there is some other code not in your example causing problems. Are there any error messages in the javascript console?
EDIT:
The updated code where A.bindClick(doSomething); is changed to A.bindClick(doSomething(1));, does not work since you're firing the doSomething method immediately and bindClick receives the return value undefined from that function call.