admin管理员组

文章数量:1391987

I have a function called "go()" which needs to be called thrice on page load. Right now I have called it on the ready event of a div with class "nav". Can someone suggest how can I call the go function thrice. Adding go() calls one after other calls it only once.

$('.nav').ready(function() {
     go();
    });

I have a function called "go()" which needs to be called thrice on page load. Right now I have called it on the ready event of a div with class "nav". Can someone suggest how can I call the go function thrice. Adding go() calls one after other calls it only once.

$('.nav').ready(function() {
     go();
    });
Share Improve this question edited Jun 3, 2009 at 13:48 Rene Saarsoo 13.9k10 gold badges62 silver badges91 bronze badges asked Jun 3, 2009 at 13:39 theranemantheraneman 1,6304 gold badges20 silver badges32 bronze badges
Add a ment  | 

5 Answers 5

Reset to default 4

Even though you say this does not work, it should...

$('.nav').ready(function() {
     go();
     go();
     go();
    });

Update: If there is an error in your go() function, it might terminate execution before go() returns for the first time. Run in Firebug and see if you are getting any errors.

$('.nav').ready(function() {
     go();
     go();
     go();
    });

Should call it three times.

As other people have stated, placing the calls one-after-the-other should have called it three times. Perhaps there something that go() is doing that is not being done more than once due to how the function is called, etc. It might be helpful to take a closer look at go() - please post that code and/or explain why you need to call it 3 times.

Like rikh sayd, you must be mistaken.

Please test that you can see three hello-s when you replace your go() function with something like this:

function go() {
  alert("hello");
}

I think I understand what you're trying to do...in the best terms as I can possibly describe since I've not taken a single jQuery class, just learned bits and pieces on my own through places like this website. I was doing something similar. I needed a datepicker to call twice, for two different fields that needed a calendar to pop up - for a check-in and check-out date in a form. In my head I did this, I made two separate scripts with two separate and different id values:

<script>
   $( function() {
     $( "#datepicker" ).datepicker();
   } );
</script>  


<script>
 $( function() {
   $( "#datepicker2" ).datepicker();
 });
</script> 

And my form in the body of my webpage looks like this:

For my Check-In Date:

<input name="DateIn" type="text" id="datepicker" style="width:90px"/>

For my Check-Out Date:

<input name="DateOut" type="text" id="datepicker2" style="width:90px">

So I guess you need to make 3 scripts each with #go1, #go2, #go3 in your head and corresponding calls in your html. I hope this helps.

本文标签: javascriptCalling a function more than once in jQueryStack Overflow