简体   繁体   中英

Calling a function more than once in jQuery

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();
    });

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.

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.

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");
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM