Solved

clearInterval not working ajax jQuery

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

Secure Your WordPress Site: 5 Essential Approaches

WordPress is the web's most popular CMS, but its dominance also makes it a target for attackers. Our eBook will show you how to:

Prevent costly exploits of core and plugin vulnerabilities
Repel automated attacks
Lock down your dashboard, secure your code, and protect your users

Question has a verified solution.

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

Requirements JQuery 1.6+ HTML CSS Introduction This article was inspired by an EE question (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Q_28372511.html) on how to make a page show some balloons animate up a page…
Introduction Chart.js, used properly, can visually add a difference to your charting applications. It engages your visitors and allows them to interact with data they otherwise wouldn't be able to without expensive and complicated systems. For this…
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…
Suggested Courses

739 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