How to call a function that is inside another function?

Ryan Long

I'm trying to make a simple image slider in jQuery && JavaScript. I started to branch out and it more of a personal thing to figure out myself and ran in to this problem. I want to call the function test after changing the var count's value. Hopefully that would send out (in this case) the next image.

var sc;
var count;

  setInterval(function test(){
      $(".slider #"+count).show("slide",{direction:'right'},500);

      $(".slider #"+count).delay(5500).hide("slide",{direction:'left'},500);

      if(count==sc || count > sc || count <= 0) {
        count=1;
      }else{
        count=count + 1;
      }
  }, 6500);

  function slider(){
    sc=$(".slider img").length;
    count= 2;

    $(".slider #1").show("fade", 500);
    $(".slider #1").delay(5500).hide("slide",{direction:'left'},500);
}


function prev() {
  count = count - 1;
  test();
}
function next() {
  count = count + 1;
  test();
}

HTML:

<div class="wrapper">
  <div class="slider">
    <img id="1" src="images/Background.JPG"/>
    <img id="2" src="images/Background2.jpeg"/>
    <img id="3" src="images/background2.png"/>
    <img id="4" src="images/profilePicture.jpg"/>
  </div>

  <a href="#" class="left" onclick="prev(); return false;">Previous</a>
  <a href="#" class="right" onclick="next(); return false;">Next</a>

</div>
Makyen

In order to access it from outside the function in which it is defined (the scope in which it was defined), you have to have a reference to it that exists in a scope which is visible to the code in which you desire to access it.

The most common way to do this is to define the function which you want to call from multiple places in a scope which is visible to all places from which you wish to call it. For example:

var count;
function test(){
    //Do stuff
}

function slider(){
    //code
    setInterval(test, 6500);
}

function prev() {
  count = count - 1;
  test();
}

function next() {
  count = count + 1;
  test();
}

An alternate method, but a bit unusual, of accomplishing the same thing would be:

var intervalFunction;

function slider(){
    //code
    setInterval(intervalFunction = function test(){
        //Do stuff
    }, 6500);
}

function prev() {
  count = count - 1;
  intervalFunction();
}

function next() {
  count = count + 1;
  intervalFunction();
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How call a function inside another function

From Dev

How to call a function that is inside another function javascript

From Dev

How do I call function inside another function?

From Dev

How do I call a function inside another function in VBA?

From Dev

How to call a function inside another function in Go language?

From Dev

how can i call function inside another function.?

From Dev

How to call a function inside another function but in a different shell

From Dev

inside if() cant call another function

From Dev

inside if() cant call another function

From Dev

Cannot call a function inside another function javascript

From Dev

How to call one method from another inside function

From Dev

How to set the global array values inside another function call

From Dev

How to call multiple functions as arguments inside another function?

From Dev

How to call a function inside SpookyJS?

From Dev

How to call function inside the IIFE

From Dev

How to call a function inside constructor

From Dev

how to call function inside a trigger?

From Dev

How to call a function inside a class

From Dev

How to call a function inside SpookyJS?

From Dev

How to call function inside the IIFE

From Dev

How to call a function inside an iframe

From Dev

How can I call a function inside of a function?

From Dev

How to call function inside a main function in MATLAB?

From Dev

How do I call a function inside a function

From Dev

How to call a function inside a main function in javascript

From Dev

How to call a inherited function inside a callback function

From Dev

how to call function that is inside a function ready()?

From Dev

How to call a function inside a main function in javascript

From Dev

how to call function inside a function with setTimeout

Related Related

  1. 1

    How call a function inside another function

  2. 2

    How to call a function that is inside another function javascript

  3. 3

    How do I call function inside another function?

  4. 4

    How do I call a function inside another function in VBA?

  5. 5

    How to call a function inside another function in Go language?

  6. 6

    how can i call function inside another function.?

  7. 7

    How to call a function inside another function but in a different shell

  8. 8

    inside if() cant call another function

  9. 9

    inside if() cant call another function

  10. 10

    Cannot call a function inside another function javascript

  11. 11

    How to call one method from another inside function

  12. 12

    How to set the global array values inside another function call

  13. 13

    How to call multiple functions as arguments inside another function?

  14. 14

    How to call a function inside SpookyJS?

  15. 15

    How to call function inside the IIFE

  16. 16

    How to call a function inside constructor

  17. 17

    how to call function inside a trigger?

  18. 18

    How to call a function inside a class

  19. 19

    How to call a function inside SpookyJS?

  20. 20

    How to call function inside the IIFE

  21. 21

    How to call a function inside an iframe

  22. 22

    How can I call a function inside of a function?

  23. 23

    How to call function inside a main function in MATLAB?

  24. 24

    How do I call a function inside a function

  25. 25

    How to call a function inside a main function in javascript

  26. 26

    How to call a inherited function inside a callback function

  27. 27

    how to call function that is inside a function ready()?

  28. 28

    How to call a function inside a main function in javascript

  29. 29

    how to call function inside a function with setTimeout

HotTag

Archive