We're a community of 1076K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,075,901 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

anonymous function with a parameter

Hi guys,
if you have an anonymous function sitting within an each function and I would like to use the this parameter within the anonymous function, how would
I do it? Currently it returns an error saying "SyntaxError: missing formal parameter setTimeout(function(this){". I am trying to apply a delay of 1 seconds
to a an image that moves in a new position
Thanks

...
$(".container > a > img").each(function(){
var opacityVal = $(this).css('opacity');
...
setTimeout(function(){
$(this).animate({left:newVal});

},1000);
});
4
Contributors
6
Replies
5 Days
Discussion Span
4 Months Ago
Last Updated
7
Views
Question
Answered
Violet_82
Master Poster
794 posts since Jan 2010
Reputation Points: 10
Solved Threads: 1
Skill Endorsements: 1

The definition of setTimeout does not have that parameter, so it would only be possible if you make a new function that would enable this. You can add the jQuery function delay.

$(this).delay(1000).animate({left:newVal});
pritaeas
Posting Prodigy
Moderator
9,271 posts since Jul 2006
Reputation Points: 1,173
Solved Threads: 1,457
Skill Endorsements: 86

You can also try the following (although this is more for educational purposes as pritaeas answer is probably best):

$(".container > a > img").each(function(){
    var opacityVal = $(this).css('opacity');
    ...
    callTimeout(this;)
});

function callTimeout(thisObj){
    setTimeout(function(){
        $(thisObj).animate({left:newVal});
    },1000);
}
stbuchok
Practically a Posting Shark
875 posts since May 2011
Reputation Points: 138
Solved Threads: 124
Skill Endorsements: 2

Watch out when you work with JavaScript. You should try not to mix pure JavaScript and JQuery syntax together, or you may get the result as you are having right now. I suggest you to use pritaeas way rather than implement it the way stbuchok does even though there is nothing wrong with stbuchok implementation. It is a good practice to do it one way for 2 good reaons -- consistancy and less confusion.

Taywin
Posting Maven
2,633 posts since Apr 2010
Reputation Points: 275
Solved Threads: 375
Skill Endorsements: 17

thank you all guys for the suggestions and avices. COuple of things:

The definition of setTimeout does not have that parameter, so it would only be possible if you make a new function that would enable this.

I suppose this translates in what stbuchok has done, another function. But what do you mean exactly by setTimeout not having the "this" parameter? Do you mean that it doesn't accept it in general or just this time because of the way I have construct the function?

You can add the jQuery function delay.

I had a look at the api, and is it correct to say that the delay method might cause problems if you run it too many times, in that it doesn't have the equivalent of the queue: false to stop the animation? For example I am thinking about the above code triggered by a button: if you go click frenzy and click the button 10-15 times then how do I make sure that jquery doesn't go nuts?
thanks

Violet_82
Master Poster
794 posts since Jan 2010
Reputation Points: 10
Solved Threads: 1
Skill Endorsements: 1

Do you mean that it doesn't accept it in general

Correct.

how do I make sure that jquery doesn't go nuts?

You could start with .stop() stopping all previous animations.

pritaeas
Posting Prodigy
Moderator
9,271 posts since Jul 2006
Reputation Points: 1,173
Solved Threads: 1,457
Skill Endorsements: 86

ok thanks

Violet_82
Master Poster
794 posts since Jan 2010
Reputation Points: 10
Solved Threads: 1
Skill Endorsements: 1
Question Answered as of 4 Months Ago by pritaeas, Taywin and stbuchok

This question has already been solved: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
 
© 2013 DaniWeb® LLC
Page rendered in 0.0794 seconds using 2.78MB