admin管理员组文章数量:1022405
I am having recursive function. it make calls every second. I want to kill that function in certain state.
function foo(){
// ajax call
//in ajax success
success: function(response){
setTimeout(function(){
foo();
},1000);
}
}
This code makes recursive call
if(user == "idile"){
//here i want to kill that foo() function
}
how can i do this ? Thanks in advance
I am having recursive function. it make calls every second. I want to kill that function in certain state.
function foo(){
// ajax call
//in ajax success
success: function(response){
setTimeout(function(){
foo();
},1000);
}
}
This code makes recursive call
if(user == "idile"){
//here i want to kill that foo() function
}
how can i do this ? Thanks in advance
Share Improve this question asked Jun 3, 2011 at 8:35 GowriGowri 16.9k27 gold badges102 silver badges162 bronze badges 1- Did you try to put that condition into the success callback function? – Gumbo Commented Jun 3, 2011 at 8:37
3 Answers
Reset to default 6Assign the timeout to a variable like this:
var timer;
function foo(){
// ajax call
//in ajax success
success: function(response){
timer = setTimeout(function(){
foo();
},1000);
}
}
and then to kill the timer:
if(user == "idile"){
clearTimeout(timer);
}
the way you do is using global variable,
var isFinish= false;
function foo(){
// ajax call
//in ajax success
success: function(response){
setTimeout(function(){
if (!isFinish)
{
foo();
}
},1000);
}
}
and then just change the isFinish to true
if(user == "idile"){
//here i want to kill that foo() function
isFinish = true;
}
When you spawn your function into a different thread, do this:
var t;
function foo()
{
// ajax call
//in ajax success
success: function(response)
{
t = setTimeout
(
function(){foo();}
,1000
);
}
}
When you want to stop it, do this:
if(user == "idile")
{
//here i want to kill that foo() function
clearTimeout(t);
}
I am having recursive function. it make calls every second. I want to kill that function in certain state.
function foo(){
// ajax call
//in ajax success
success: function(response){
setTimeout(function(){
foo();
},1000);
}
}
This code makes recursive call
if(user == "idile"){
//here i want to kill that foo() function
}
how can i do this ? Thanks in advance
I am having recursive function. it make calls every second. I want to kill that function in certain state.
function foo(){
// ajax call
//in ajax success
success: function(response){
setTimeout(function(){
foo();
},1000);
}
}
This code makes recursive call
if(user == "idile"){
//here i want to kill that foo() function
}
how can i do this ? Thanks in advance
Share Improve this question asked Jun 3, 2011 at 8:35 GowriGowri 16.9k27 gold badges102 silver badges162 bronze badges 1- Did you try to put that condition into the success callback function? – Gumbo Commented Jun 3, 2011 at 8:37
3 Answers
Reset to default 6Assign the timeout to a variable like this:
var timer;
function foo(){
// ajax call
//in ajax success
success: function(response){
timer = setTimeout(function(){
foo();
},1000);
}
}
and then to kill the timer:
if(user == "idile"){
clearTimeout(timer);
}
the way you do is using global variable,
var isFinish= false;
function foo(){
// ajax call
//in ajax success
success: function(response){
setTimeout(function(){
if (!isFinish)
{
foo();
}
},1000);
}
}
and then just change the isFinish to true
if(user == "idile"){
//here i want to kill that foo() function
isFinish = true;
}
When you spawn your function into a different thread, do this:
var t;
function foo()
{
// ajax call
//in ajax success
success: function(response)
{
t = setTimeout
(
function(){foo();}
,1000
);
}
}
When you want to stop it, do this:
if(user == "idile")
{
//here i want to kill that foo() function
clearTimeout(t);
}
本文标签: jquerykill JavaScript recursive functionStack Overflow
版权声明:本文标题:jquery - kill JavaScript recursive function - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745572846a2156840.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论