Tag Archive for Ujjwal Prajapati

Variable scope in js and ajax function

Often, when we encounter a situation when the link has to be followed with some condition check in server, we could skip the form and use the simple a tag to navigate the link with javascript/ajax. You could simple use the ajax call to request the server file and then get the response and based on the response you could either follow the link or block it.

But in doing so, you may have to share the variable between javascript function and ajax function. This arises the variable scope issue in javascript. We have to use the scope operator to point the variable properly. In our case, we have defined the scope as var _this = this; . Now we could use this operator to access the js variable inside the ajax function.

The a tag will navigate the link by default so in order to block it we will use the onClick event. This event will fire and the link will be navigated if the response is true and our function returns either true or false based on the server response. And that’s that. Simple huh!! The code should be self explanatory. But just in case you need my help, you know where to find me else please drop a comment 🙂

<script src="http://code.jquery.com/jquery-1.10.1.min.js"></script>
<script type="text/javascript">
function checkA() {
	var _this = this;
	var check;

	$.ajax({ 
		type: 'POST',
		url: 'checkhere.php',
		async: false,
		success: function (response) {			
			if(response) {
				_this.check = true;
			}else {
				_this.check = false;
			}
		}
	});
	
	return _this.check;
}
</script>
<a href="gohere.php" onclick="return checkA();">Follow Link with Check</a>

Pause and Cancel Operation using Ajax

Recently I came accross the situation when I need to upload zip file containing multiple files and have to compute some operation for each file. The upload process could be achieved easily using ajax file upload. But the part that obstruct me is the pause and cancel operation in ajax.

I googled it a lot and there is no single tutorial on that topic. So, I decided to write it myself. Below is just the code snippet for pause and cancel operation. You have to call the ajax request function recursively in order to achieve this operation until all the queue are processed. It checks the pause status and only operates if the process is resume. The code should be fairly simple and self explanatory.

<script>
$(document).ready(function() {
	var xhr;
	var pause = false;
	var counter = 0;
	var lastEncounter = 0;
	var arr = ["1","2","3","4"];
				
	function myAjaxRequest(value) {	
		if(!pause) {
			lastEncounter = value;
			xhr = $.ajax({
				url: 'test.php',
				type: 'GET',
				data: "",
				success: function(data) {
					counter++;		
					if(counter < arr.length) {
						myAjaxRequest(arr[counter]);
					}
				}							                    
       });				
		}					
	}
				
	$("#start").click(function(e) {	
		myAjaxRequest(arr[0]);				
	});				
						
	$("#cancel").click(function(e) {
		xhr.abort();
	});
				
	$("#pause").click(function(e) {
		pause = true;
	});
				
	$("#resume").click(function(e) {
		pause = false;
		myAjaxRequest(lastEncounter + 1);
	});
});
</script>

<input type="button" value="Start" id="start">
<input type="button" value="Cancel" id="cancel">
<input type="button" value="Pause" id="pause">
<input type="button" value="Resume" id="resume">

Dr. House – A Character As An Inspiration

Many of the people may not know who Dr. House is, but those of you who knows, are among the luckiest people to recognize such a great character. Yes, Dr. House is the main character of the most popular series HOUSE M.D.   I have been following this series for years now and it has also came to it’s shore now. But then, every good thing has to come to the end. And life just goes on!

Dr. House played by British Actor “Hugh Laurie“, stood as an icon and the inspiration to many of the viewers. For some he may just be an arrogant psycho who doesn’t listen to anyone or does his work in his own way troubling his fellows and colleagues. I am sure, those don’t understand or like his perspective. That means they still need time to realize; I am sure they will one day.

Speaking of myself, he is an inspiration who has shown me different perspective of life with different ways to look upon and pursue things. It is his sarcasm and perspective that lures me a lot. His way of caring, his way of treating people and his way of finding solutions to the problem, that is just unthinkable. It is not necessary that you look your life from someone else’s perspective but once you do, you find yourself differently.

I still remember when he tries to save marriage of his only friend Wilson when he says “If you stay here, that’s just the fight; If you find the new apartment, that’s a divorce”. He presents absolutely different perspective of caring. We as a human being don’t need no words to care and love, it needs action and this is what’s lagging in today’s world. People say words they don’t mean and people believe in words said. None care about the action made.

“Everybody Lies”. Indeed that is a fact. There are no people who don’t lie and Lies are so spread that even if people speak of the truth, are considered lies. We all lie just to get rid of anything or everything. And I always wonder, if we lie to people we love or care about, who are we going to tell the truth about.

“You’re a moron”, Of course, every one of us is a moron who don’t seem to live a life we deserve. His way of handling people and making them capable of dealing with things is just remarkable. His using of sarcasm on the fact which we find hard to realize is just amazing. People don’t realize what they have and they are capable of. They are one of the strongest being in this planet and yet they seem to be get fuzzy with all these unnecessary fear and emotions.

“Life is Pain”, Needless to say. Everyone has a fear within and feel the pain. The one who deals with it can live the life he wants. And yes, one does not need to be happy to live.

“Dr. House” as a character has presented different perspective on our way of living. It is indeed a fact that everyone has House hidden inside. The question is “Are you strong enough to be one?”

000webhost logo