Solved

clearInterval not working ajax jQuery

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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

DOM Attributes and Properties treatment with jQuery 1.6 by Ivo Stoykov jQuery (http://jquery.com/) 1.6 introduces .prop() (http://api.jquery.com/prop/) and .removeProp() (http://api.jquery.com/removeProp/) methods which allow modifying or removi…
Introduction JSON is an acronym for JavaScript Object Notation.  It is a text-string data transport mechanism, capable of representing simple or complex data structures in a consistent and easy-to-read manner.  Similar in concept to XML, but more e…
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…

762 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

23 Experts available now in Live!

Get 1:1 Help Now