2

I have a function, that is called by interval function, as well on click on a link, my link will clear the interval, and call the same function again.

calling this types will do the different work, but i don't know how to find, whether my interval function is calling my function or onclick function calling my function..

How can i find the caller of my function?

This is my function

function slideshow (){
    $('#slide_viewer ul').css({marginLeft : -(slideWidth * currentSlide)+'px'});
    if(currentSlide == (totalSlides-1)){
        currentSlide = 0;
    }else{
        currentSlide++;
    }

}
var myInterval = setInterval(slideshow,3000);

$('a.control').click(function(){
    currentSlide = ($(this).attr('id') == "prev") ? (currentSlide = currentSlide-1) : (currentSlide = currentSlide+1);
    slideshow ();
})
3gwebtrain
  • 14,640
  • 25
  • 121
  • 247

3 Answers3

2

You can use

  arguments.callee.caller

to find who is calling your function.

A even simpler solution would be to flip a global variable when you are calling slideShow(). So when setInterval calls it your global variable will be false.

You can also use a closure and an argument to differentiate the cases as well

function slideshow(var autocall)
{
//your stuff depending of autocall false or true
}

var automaticall = function() 
{
   var autocall = true;
   slideshow(autocall);
}

var myinterval  = setInterval( automaticall , 3000 );


$('a.control').click(function(){
    currentSlide = ($(this).attr('id') == "prev") ? (currentSlide = currentSlide-1) : (currentSlide = currentSlide+1);
    slideshow (false);
})
parapura rajkumar
  • 24,045
  • 1
  • 55
  • 85
1

The comment links to a good post, or you could pass something to your function from the interval call:

var myInterval = setInterval(function()
{

    slideshow("interval");

},3000);
Alex
  • 7,320
  • 1
  • 18
  • 31
0

There is the callee and caller functions. But these are depreciated in most browsers today T.T

PicoCreator
  • 9,886
  • 7
  • 43
  • 64