Solved

clearInterval not working ajax jQuery

Posted on 2014-12-30
2
486 Views
Last Modified: 2014-12-30
Hi,

In this code, the line clearInterval(myVar) never stops the interval, any idea?

<script>

var myVar = "";

$("li").bind("taphold",function(event){	
	
	alert($(this).attr("numeroEvento"));

var formData = {evento: $(this).attr("numeroEvento")}; //Array 
 
$.ajax({
    url : "medicionInsert.php",
    type: "POST",
    data : formData,
    success: function(data, textStatus, jqXHR)
    {
        alert (data);
		var myVar = setInterval(function(){ test() }, 3000);
		
    },
    error: function (jqXHR, textStatus, errorThrown)
    {
 
    }
});

	/*if($(this).attr("enviado") == 'no'){
		
		
	var x = $(this).html();
	
	$(this).html(x + "<p>Evento enviado a medicion</p>");
	
	$(this).attr("enviado","si");
	
	alert("EVENTO ENVIADO A MEDICION");
	
}else{
	
	alert($(this).attr("numeroEvento")+" ya fue enviado a medicion");
		
}*/		
	
	
	});
	
	var test = function(){
	
	$.ajax({
    url : "pararlectura.php",
    type: "POST",
    success: function(data, textStatus, jqXHR)
    {
		var prueba = data
		alert(prueba)
		
        if ($.trim(prueba) == "YA"){
			
			alert ("test");
			clearInterval(myVar);
		}
		
		
    },
    error: function (jqXHR, textStatus, errorThrown)
    {
 
    }
});	
	
	}
	
	
	
</script>

Open in new window

0
Comment
Question by:joyacv2
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 82

Accepted Solution

by:
leakim971 earned 500 total points
ID: 40523489
it's a scope issue
use :
myVar = setInterval(function(){ test() }, 3000);

If it doesn't work, use :
	window.myVar = "";

	$("li").bind("taphold",function(event){	
		
		alert($(this).attr("numeroEvento"));

		var formData = {evento: $(this).attr("numeroEvento")}; //Array 
	 
		$.ajax({
			url : "medicionInsert.php",
			type: "POST",
			data : formData,
			success: function(data, textStatus, jqXHR)
			{
				alert (data);
				window.myVar = setInterval(function(){ test() }, 3000);
				
			},
			error: function (jqXHR, textStatus, errorThrown)
			{
		 
			}
		});

	});
	
	var test = function(){
	
		$.ajax({
			url : "pararlectura.php",
			type: "POST",
			success: function(data, textStatus, jqXHR)
			{
				var prueba = data;
				alert(prueba)
				
				if ($.trim(prueba) == "YA") {
					alert ("test");
					clearInterval(window.myVar);
				}
			},
			error: function (jqXHR, textStatus, errorThrown)
			{
		 
			}
		});	
	
	}

Open in new window

0
 
LVL 1

Author Closing Comment

by:joyacv2
ID: 40523499
works excellent!
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

What is a Lightbox? A Lightbox is the effect you see when you click, for example, an image and the screen fades out and up pops the same image but in its full size dimensions. There are lots of Lightbox effects for jQuery. Problem is they are a…
Introduction Knockoutjs (Knockout) is a JavaScript framework (Model View ViewModel or MVVM framework).   The main ideology behind Knockout is to control from JavaScript how a page looks whilst creating an engaging user experience in the least …
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question