Solved

clearInterval not working ajax jQuery

Posted on 2014-12-30
2
419 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
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

by Julian Matz As of jQuery (http://jquery.com/) 1.4 the .live() method (http://api.jquery.com/live/) supports custom events as well as some standard JavaScript events that it previously didn't support. Among these is the submit event. Unlike …
There are a couple ways to attach a JavaScript function to dynamically created elements. You can make a new script for each element as it’s created or you can use delegation. Delegation allows a single script that is added at page creation to mat…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. 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.: (CODE)
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…

914 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now