Solved

clearInterval not working ajax jQuery

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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

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 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…

756 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