How to find, which one is calling my function?
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 ();
})
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);
})
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);
There is the callee and caller functions. But these are depreciated in most browsers today T.T
精彩评论