Thursday, July 31, 2008

Passing function as callback to another function

A callback is a function that is passed as an argument to another function and is executed after its parent function has completed.

Another important thing to know is how to properly pass the callback. This is where I have often forgotten the proper syntax.

Callback without arguments

For a callback with no arguments you pass it like this:

myFunction(myCallBack);

Note that the second parameter here is simply the function name (but not as a string and without parentheses). Functions in Javascript are 'First class citizens' and so can be passed around like variable references and executed at a later time.

Callback with arguments

"What do you do if you have arguments that you want to pass?", you might ask yourself.

Wrong

The Wrong Way (will not work!)


myFunction(myCallBack(param1, param2));


This will not work because it calls myCallBack(param1, param2) and then passes the return value as the second parameter to myFunction.

Right

The Right Way ( note the use of function(){ preceding myCallBack() )


myFunction(function(){
myCallBack(param1, param2);
});

89 comments:

Zang said...

Chamnap,

Thanks for this example!

I was running into a problem where calling myFunction(myCallBack('someArg')) was actually executing myCallBack('someArg') before executing myFunction.

Modifying the code to prepend function(){... to my myCallBack call allows me to pass the reference around nicely. Thanks!

Erin
Seattle, WA

chamnap said...

You're welcome.

Anonymous said...

… Unbelievable , but I just found software which can do all hard work promoting your chamnapchhorn.blogspot.com website on complete autopilot - building backlinks and getting your website on top of Google and other search engines 1st pages, so your site finally can get laser targeted qualified traffic, and so you can get lot more visitors for your website.

YEP, that’s right, there’s this little known website which shows you how to get to the top 10 of Google and other search engines guaranteed.

I used it and in just 7 days… got floods of traffic to my site...

…Well check out the incredible results for yourself -
http://magic-traffic-software.com

I’m not trying to be rude here, but I believe when you find something that finally works you should share it…

…so that’s what I’m doing today, sharing it with you:

http://magic-traffic-software.com

Take care - your friend Jennifer

Anonymous said...

After getting more than 10000 visitors/day to my website I thought your chamnapchhorn.blogspot.com website also need unstoppable flow of traffic...

Use this BRAND NEW software and get all the traffic for your website you will ever need ...

= = > > http://get-massive-autopilot-traffic.com

In testing phase it generated 867,981 visitors and $540,340.

Then another $86,299.13 in 90 days to be exact. That's $958.88 a
day!!

And all it took was 10 minutes to set up and run.

But how does it work??

You just configure the system, click the mouse button a few
times, activate the software, copy and paste a few links and
you're done!!

Click the link BELOW as you're about to witness a software that
could be a MAJOR turning point to your success.

= = > > http://get-massive-autopilot-traffic.com

Anonymous said...

Pretty section of content. I just stumbled upon your site and in accession capital to assert that I acquire actually enjoyed
account your blog posts. Any way I will be subscribing to your augment and even I
achievement you access consistently rapidly.
My page :: shopmcanow.com

Anonymous said...

This website certainly has all the information and facts I wanted concerning this subject and
didn't know who to ask.
Have a look at my page ; Nutrition

Anonymous said...

Fine way of telling, and fastidious paragraph to take data about my presentation focus, which i am going to deliver in university.
Look at my web-site ... http://www.candecarpetsandvents.com

Anonymous said...

If you desire to get much from this piece of writing then you have to apply
such strategies to your won webpage.
My page Http://Glowballoonsusa.Com/Rsscategoryproducts.Sc?CategoryId=1

Anonymous said...

Please let me know if you're looking for a writer for your weblog. You have some really great articles and I feel I would be a good asset. If you ever want to take some of the load off, I'd absolutely love to
write some material for your blog in exchange for a link back to mine.
Please blast me an e-mail if interested. Thank you!
Feel free to surf my homepage :: natural health

Anonymous said...

wonderful put up, very informative. I ponder why the opposite experts of this sector do not
realize this. You must continue your writing. I am confident, you have a
huge readers' base already!
My page 3dtv

Anonymous said...

Hi! I'm at work surfing around your blog from my new apple iphone! Just wanted to say I love reading your blog and look forward to all your posts! Keep up the excellent work!
Here is my weblog : Vertnet

Anonymous said...

I was able to find good advice from your blog articles.
Here is my web site ; Popflops

Anonymous said...

My developer is trying to convince me to move to .
net from PHP. I have always disliked the idea because of the expenses.

But he's tryiong none the less. I've been using WordPress on several websites for about a
year and am worried about switching to another platform.
I have heard good things about blogengine.net. Is there a way I can import all my wordpress content into it?
Any help would be greatly appreciated!
My page ... services

Anonymous said...

Very nice post. I just stumbled upon your blog and wanted to say
that I have truly enjoyed surfing around your blog posts.
After all I'll be subscribing to your feed and I hope you write again very soon!
Also visit my web-site : baby fishing shirt

Anonymous said...

Neat blog! Is your theme custom made or did
you download it from somewhere? A design like yours with a
few simple adjustements would really make my blog jump out.
Please let me know where you got your theme. Many thanks
Look at my blog ; ASSI PLAZA

Anonymous said...

Very nice blog post. I absolutely love this website. Continue
the good work!
Also visit my blog post : www.divinedestinycareercoaching.com

Anonymous said...

I will right away seize your rss feed as I can't in finding your email subscription hyperlink or e-newsletter service. Do you've any?
Please permit me understand in order that I could subscribe.
Thanks.
Also visit my page :: sanstreet.com.sg

Anonymous said...

Please let me know if you're looking for a article author for your weblog. You have some really good posts and I believe I would be a good asset. If you ever want to take some of the load off, I'd
absolutely love to write some articles for your blog in exchange for a link back
to mine. Please send me an e-mail if interested. Many thanks!
Visit my site ; Teamspeak and Shoutcast Servers

Anonymous said...

At this time I am going to do my breakfast, afterward
having my breakfast coming over again to read other news.
Also visit my webpage - bulk cotton candy

Anonymous said...

What's Happening i am new to this, I stumbled upon this I have found It absolutely useful and it has aided me out loads. I'm
hoping to contribute & help different customers like its aided
me. Good job.
My web-site papillex.com

Anonymous said...

Howdy I am so grateful I found your website, I really
found you by error, while I was searching on Digg for something else, Anyhow I am here now and would just like
to say thanks for a remarkable post and a all round interesting blog (I also love
the theme/design), I donít have time to look over it all at the minute but
I have book-marked it and also added your RSS feeds, so when I have time I will be
back to read a great deal more, Please do keep up the superb work.


Here is my website :: Grapevine furnace repair services

Anonymous said...

Greetings. I am wondering if you'd be interested in doing a link swap? I see your website: http://www.blogger.com/comment.g?blogID=6761142148052793737&postID=3530293178756864170 and my website are primarily based around the same subject matter. I'd really like
to switch links or possibly guest author a article for you.
Here is my personal contact: leonarogers@vegemail.
com. You should contact me if you're even remotely interested. Appreciate it.

Check out my web blog - gas furnace evolution system plus
Also see my page - how gas furnace works

Anonymous said...

Hi! This is my 1st comment here so I just wanted to give a quick
shout out and say I really enjoy reading your blog posts.
Can you recommend any other blogs/websites/forums that cover the same
topics? Thanks a ton!

My site: youtube.com

Anonymous said...

Hi your internet site url: http://www.blogger.com/comment.

g?blogID=6761142148052793737&postID=3530293178756864170 seems to be redirecting to a completely different
webpage when I click the home page button. You may want to have this checked.



Have a look at my page ... dallas home repair

Anonymous said...

I'm truly enjoying the design and layout of your blog. It's a very easy on the
eyes which makes it much more pleasant for me
to come here and visit more often. Did you hire out a developer to create your theme?
Great work!

Feel free to visit my page gymnastics clothing
my website - girls gymnastics clothes

Anonymous said...

Hello there! Would you mind if I share your blog with my twitter group?
There's a lot of folks that I think would really enjoy your content. Please let me know. Many thanks

Feel free to surf to my web-site; inexpensive workout gear
Also see my web site - mosfet switch signals

Anonymous said...

I have been surfing online more than 4 hours
today, yet I never found any interesting article like yours.
It's pretty worth enough for me. Personally, if all website owners and bloggers made good content as you did, the net will be a lot more useful than ever before.

Review my site ... sushi restaurants in oviedo fl

Anonymous said...

Incredible quest there. What happened after? Take care!


Here is my blog post - true backstory of the rugrats

Anonymous said...

Hi, I believe your site could possibly be having internet browser compatibility
problems. When I look at your site in Safari, it looks fine however, when opening in I.
E., it has some overlapping issues. I merely wanted to provide you with a quick heads up!

Apart from that, wonderful site!

Here is my web site chest spasms during pregnancy

Anonymous said...

I have read so many content regarding the blogger lovers but
this piece of writing is really a fastidious post, keep it up.


Here is my weblog :: www.journalhome.com

Anonymous said...

Howdy great blog! Does running a blog such as this take a large amount of work?
I have virtually no knowledge of coding however I had been hoping to start my own blog soon.
Anyhow, should you have any suggestions or techniques for new blog owners
please share. I know this is off topic however I just wanted to ask.
Appreciate it!

my website - girls activewear sets

Anonymous said...

I know this if off topic but I'm looking into starting my own weblog and was wondering what all is needed to get setup? I'm assuming
having a blog like yours would cost a pretty penny?
I'm not very internet smart so I'm not 100% positive. Any suggestions or advice would be greatly appreciated. Thanks

Here is my website: dance shoes
Also see my web page :: studio tennis shoes

Anonymous said...

It's actually a cool and helpful piece of information. I am satisfied that you shared this helpful information with us. Please stay us informed like this. Thank you for sharing.

My homepage: http://www.Orangecountyfloridabusinesslist.com/

Anonymous said...

Today, while I was at work, my cousin stole my apple ipad and tested to see if it
can survive a 40 foot drop, just so she can be a youtube
sensation. My apple ipad is now broken and she
has 83 views. I know this is completely off
topic but I had to share it with someone!

my web-site: adidas gym clothes for women

Anonymous said...

Hey this is kind of of off topic but I was wanting to know if blogs
use WYSIWYG editors or if you have to manually code with
HTML. I'm starting a blog soon but have no coding experience so I wanted to get guidance from someone with experience. Any help would be enormously appreciated!

Take a look at my page youtube.com

Anonymous said...

Heya this is kinda of off topic but I was wondering if blogs use WYSIWYG editors
or if you have to manually code with HTML.
I'm starting a blog soon but have no coding expertise so I wanted to get guidance from someone with experience. Any help would be greatly appreciated!

Stop by my page ... silicon wafer production equipment

Anonymous said...

I know this if off topic but I'm looking into starting my own blog and was wondering what all is required to get setup? I'm assuming having a blog like yours
would cost a pretty penny? I'm not very internet smart so I'm not
100% positive. Any recommendations or advice would be greatly appreciated. Cheers

my site; wafer production process

Anonymous said...

Right now it seems like Expression Engine is
the best blogging platform out there right now. (from what I've read) Is that what you are using on your blog?

My page: device driver software for lg android phone

Anonymous said...

Hey there, I think your website might be having browser compatibility issues.
When I look at your blog site in Firefox, it looks fine but
when opening in Internet Explorer, it has some overlapping.
I just wanted to give you a quick heads up! Other then that, wonderful blog!



my web page digital cameras on sale this week

Anonymous said...

I don't even know how I ended up here, but I thought this post was great. I don't know who you are but definitely you're going to a famous blogger if you aren't already ;) Cheers!



Check out my webpage; callaway golf hats for men

Anonymous said...

We're a bunch of volunteers and starting a brand new scheme in our community. Your website provided us with useful info to work on. You've performed a formidable process and our entire neighborhood can
be grateful to you.

Feel free to visit my blog post - bancuri seci noi

Anonymous said...

What's up everyone, it's my first pay a quick visit at this web site,
and post is actually fruitful for me, keep up posting these types of articles
or reviews.

Review my web-site - jocuri online friv

Anonymous said...

What i do not realize is actually how you
are not actually a lot more neatly-liked than you might be
now. You're so intelligent. You know thus significantly when it comes to this subject, made me personally consider it from so many numerous angles. Its like men and women aren't interested
except it is something to accomplish with Woman gaga!
Your individual stuffs nice. At all times deal
with it up!

Feel free to visit my site: jocuri on line cu impuscaturi

Anonymous said...

Your style is unique compared to other folks I've read stuff from. Many thanks for posting when you've got the opportunity,
Guess I will just bookmark this web site.

Here is my blog post; Juegos Gratis

Anonymous said...

I'm not sure where you are getting your info, but good topic. I needs to spend some time learning much more or understanding more. Thanks for fantastic information I was looking for this info for my mission.

Also visit my page - back muscles anatomy

Anonymous said...

Whoa! This blog looks exactly like my old one!
It's on a totally different subject but it has pretty much the same layout and design. Superb choice of colors!

Also visit my web-site ... www.Yelp.fr

Anonymous said...

Hello, its pleasant post on the topic of media print, we all know media is a impressive source of data.


Visit my website ... Commercial Led Lighting

Anonymous said...

It's really very complicated in this full of activity life to listen news on Television, thus I simply use world wide web for that purpose, and take the latest news.

Here is my homepage :: Golf Schools Las Vegas

Anonymous said...

Hi there! Do you use Twitter? I'd like to follow you if that would be okay. I'm definitely enjoying your blog
and look forward to new posts.

Also visit my website: http://mimiminneapolis5i7.newsvine.com/

Anonymous said...

I know this web page provides quality dependent content and extra information, is there any other website which offers these stuff in quality?



Take a look at my weblog: jocuri mario si luigi

Anonymous said...

Excellent weblog here! Also your site lots up fast!
What host are you the usage of? Can I am getting your associate link for your host?

I want my website loaded up as fast as yours
lol

Also visit my web page right scapula pain during pregnancy

Anonymous said...

Hi! Do you know if they make any plugins to safeguard against hackers?
I'm kinda paranoid about losing everything I've worked hard on.
Any recommendations?

My web blog :: wallinside.com

Anonymous said...

Hey administrator, I just wanted to give you a quick heads
up that your current Website url: http://www.blogger.

com/comment.g?blogID=6761142148052793737&postID=3530293178756864170 is
being flagged as a possibly malicious blog in my browser opera.
I would highly recommend having somebody look into it.

You can certainly lose a lot of website visitors due to this problem.
Best of Luck.

Also visit my page; mimiminneapolis40.tumblr.com

Anonymous said...

Howdy. I'm sorry to trouble you but I ran across your weblog and noticed you are using the exact same template as me. The only issue is on my website, I'm unable to get
the style and design looking like yours. Would you mind e-mailing me at:
lawanna_cone@gmail.com so I can get this figured out.

By the way I have bookmarked your site: http:
//www.blogger.com/comment.g?blogID=6761142148052793737&postID=3530293178756864170 and
will be visiting often. Thanks!

Take a look at my weblog ... silicon valley bank wiki

Anonymous said...

Keep on writing, great job!

Here is my website jocuri cu impuscaturi .ro

Anonymous said...

Good post! We are linking to this great content on our website.

Keep up the good writing.

My web blog ... Juegos Gratis

Anonymous said...

I read this post completely about the comparison of
most up-to-date and preceding technologies, it's awesome article.

my website: history of physical therapy in ancient rome

Anonymous said...

Hey, I think your website might be having browser compatibility
issues. When I look at your website in Ie, it looks fine but
when opening in Internet Explorer, it has some overlapping.
I just wanted to give you a quick heads up!
Other then that, fantastic blog!

My blog - http://www.youtube.com/

Anonymous said...

Do you have any video of that? I'd like to find out some additional information.

Feel free to surf to my blog post Chiropractor

Anonymous said...

Wonderful goods from you, man. I have understand your stuff previous to and you're just extremely wonderful. I actually like what you have acquired here, certainly like what you are saying and the way in which you say it. You make it entertaining and you still care for to keep it smart. I can't wait to read much more from
you. This is actually a wonderful web site.

My blog - Orlando Sushi

Anonymous said...

WOW just what I was searching for. Came here by searching for golf bag cart

Feel free to visit my page: Short Game Schools

Anonymous said...

This excellent website certainly has all of the info I wanted about this subject and didn't know who to ask.

Here is my web site: St Cloud Floral

Anonymous said...

Hi, for all time i used to check web site posts here early in the dawn, because i enjoy to find out more and more.


My web page - montaż klimatyzacji warszawa

Anonymous said...

I visited several web sites except the audio feature for audio songs current at this
web page is truly excellent.

Here is my blog Serwis klimatyzacji warszawa

Anonymous said...

Nice blog! Is your theme custom made or did you download
it from somewhere? A theme like yours with a few simple tweeks would really make my blog shine.
Please let me know where you got your theme. Kudos

Also visit my weblog; spine injury specialist

Anonymous said...

Thank you for sharing your thoughts. I truly appreciate your efforts and I
will be waiting for your further write ups thanks once again.


Here is my webpage golf gloves

Anonymous said...

With havin so much content and articles do you ever run into any
problems of plagorism or copyright violation? My site has a lot
of exclusive content I've either authored myself or outsourced but it appears a lot of it is popping it up all over the web without my authorization. Do you know any ways to help stop content from being stolen? I'd
certainly appreciate it.

Here is my page ... easy sushi recipes for beginners

Anonymous said...

I'm impressed, I have to admit. Rarely do I come across a blog that's both equally educative and
engaging, and let me tell you, you have hit the nail
on the head. The problem is something too few men and women are
speaking intelligently about. I am very happy that I found this during my hunt for something relating to this.


Here is my site: golf simulator prices calgary

Anonymous said...

Really when someone doesn't be aware of after that its up to other users that they will assist, so here it takes place.

Here is my blog post klimatyzacja

Anonymous said...

What's up mates, good piece of writing and nice urging commented here, I am in fact enjoying by these.

Also visit my site ... klimatyzacja

Anonymous said...

After exploring a handful of the blog articles on your site, I seriously like your way of
blogging. I saved as a favorite it to my bookmark site list and
will be checking back in the near future. Please check out my
web site too and let me know your opinion.

Review my web-site - klimatyzacja

Anonymous said...

Hi there mates, its fantastic piece of writing regarding
educationand fully defined, keep it up all the time.



Feel free to visit my web blog :: klimatyzacja

Anonymous said...

Aw, this was an incredibly nice post. Spending some time and actual effort to generate a
good article… but what can I say… I hesitate a lot and don't manage to get anything done.

My web-site - klimatyzacja

Anonymous said...

Its like you read my mind! You appear to know so much about this, like you wrote the book in it or something.
I think that you can do with a few pics to drive the message home a little bit, but instead of that,
this is excellent blog. An excellent read. I will certainly be back.



Here is my weblog klimatyzacja

Anonymous said...

Hey There. I discovered your blog the usage of msn. That is an extremely neatly written
article. I will be sure to bookmark it and come back to learn extra of your useful information.
Thanks for the post. I'll certainly comeback.

Feel free to surf to my web-site: klimatyzacja

Anonymous said...

Howdy, I believe your site could be having web browser compatibility issues.
When I take a look at your blog in Safari,
it looks fine however when opening in Internet Explorer, it's got some overlapping issues. I merely wanted to give you a quick heads up! Apart from that, fantastic website!

My site klimatyzacja

Anonymous said...

Hey! It seems as though we both have a interest
for the same thing. Your blog, "Blogger: Web Development" and mine are very similar.
Have you ever thought of authoring a guest post for a related website?
It will unquestionably help gain publicity to your blog (my site recieves
a lot of traffic). If you happen to be interested, contact
me at: noreen_shapiro@zoho.com. Thank you

my webpage; lemon law in indiana

Anonymous said...

Hey there! It seems as though we both have a passion for the same thing.
Your blog, "Blogger: Web Development" and mine are very similar.
Have you ever thought about writing a guest write-up for a
similar blog? It will surely help gain publicity to
your blog (my website recieves a lot of traffic).
If you happen to be interested, e-mail me at: leanna.
pinckney@googlemail.com. Thank you so much

Here is my web site :: cooking schools ohio

Anonymous said...

Write more, thats all I have to say. Literally, it seems as though you
relied on the video to make your point. You definitely know
what youre talking about, why throw away your intelligence on just posting videos to
your site when you could be giving us something
informative to read?

Here is my web page :: microsoft exchange spam

Anonymous said...

I'm not sure exactly why but this site is loading incredibly slow for me. Is anyone else having this problem or is it a issue on my end? I'll check back later on and
see if the problem still exists.

Feel free to surf to my webpage; refrigerator and heat pump difference

Anonymous said...

Nice post. I was checking continuously this weblog and I'm impressed! Very helpful info specially the remaining part :) I deal with such information a lot. I used to be seeking this particular info for a very long time. Thanks and good luck.

My webpage :: kitchen remodeling ideas on a budget pictures

Anonymous said...

When I initially commented I clicked the "Notify me when new comments are added" checkbox and now each
time a comment is added I get several e-mails with the same
comment. Is there any way you can remove me from that service?

Thanks a lot!

Take a look at my website: orangecountyhank71.edublogs.org

Anonymous said...

Thank you for sharing your info. I really appreciate your efforts and I
am waiting for your next write ups thank you once again.



Feel free to visit my web blog: home remodeling loan calculator

Anonymous said...

Hey there! I hope you do not mind but I decided to publish your blog:
http://www.blogger.com/comment.g?blogID=6761142148052793737&postID=3530293178756864170
to my on-line directory. I used, "Blogger: Web Development" as your weblog title.

I hope this is acceptable with you. In the event you'd like me to change the title or perhaps remove it entirely, e-mail me at gustavofarias@bigstring.com. Thank you.

My blog post - appliance repair company

Anonymous said...

Hi friends, nice paragraph and nice urging commented at
this place, I am truly enjoying by these.

Feel free to visit my webpage ... holly sonders golf channel wikipedia

Unknown said...
This comment has been removed by the author.
Unknown said...
This comment has been removed by the author.
Legal Process Outsourcing said...

Legal Process Outsourcing Services,Webinar On Patenting strategies for Start-ups in the IT and ITES domains

Anonymous said...

Great looking web site. Presume you did a bunch of your very own html coding
,We at Addhunters shifted this service to a level much higher than the broker concept. you can see more details like this article Property for sale Good location

Subscribe in a Reader