milosev.com
  • Home
    • List all categories
    • Sitemap
  • Downloads
    • WebSphere
    • Hitachi902
    • Hospital
    • Kryptonite
    • OCR
    • APK
  • About me
    • Gallery
      • Italy2022
      • Côte d'Azur 2024
    • Curriculum vitae
      • Resume
      • Lebenslauf
    • Social networks
      • Facebook
      • Twitter
      • LinkedIn
      • Xing
      • GitHub
      • Google Maps
      • Sports tracker
    • Adventures planning
  1. You are here:  
  2. Home

Javascript is not executed in background

Details
Written by: Stanko Milosev
Category: JavaScript
Published: 27 January 2015
Last Updated: 27 January 2015
Hits: 4423

Check this code:

<!DOCTYPE html>
<html>

	<head>
		<script type="text/javascript" src="/jquery-2.1.3.js"></script>
		<script type="text/javascript" src="/index.js"></script>
	</head>
	
	<body>
		Here my web site will be loaded:
		<div id="myWebSite"></div>
	</body>

</html>

JS:

window.onload=function () {
	var contentURI = 'http://localhost/jasmineClock/file.html',
		isLoadingStarted,
		loadingTimeOutHandle;
		
	isLoadingStarted = true;
	
	setTimeout(function () {
		$( "#myWebSite" ).html( $( "#myWebSite" ).html() + "<p>setTimeout</p>" );
		isLoadingStarted = false;
	}, 100);
	

	$.get(contentURI, function (data) {
		$( "#myWebSite" ).html( $( "#myWebSite" ).html() + data );
	});

	isLoadingStarted = false;
}

file.html which will be loaded:

<!DOCTYPE html>
<html>

	<head>
		<script type="text/javascript">
			var i = 0;
			while (i < 1000000000) {
				i++
			}
		</script>
	</head>
	
	<body>
		I will be loaded.
	</body>

</html>

If you execute this code, you will see that setTimeout will be executed AFTER ajax call is finished, delete javascript part from file.html, and maybe add some long lorem ipsum, just to slow down execution, and you will see that setTimeout will be executed beofre ajax call.

Here you can see example with javascript, and here you can see example with long lorem ipsum.

 

To prototype or not

Details
Written by: Stanko Milosev
Category: JavaScript
Published: 15 January 2015
Last Updated: 15 January 2015
Hits: 4117

Consider following example:

var Foo=function () {
	var number = 1;
	self.number = 1;
}

Foo.prototype.bar = function () {
	console.log("test");
};

var foo = new Foo();

foo.bar();

Notice here that in line Foo.prototype.bar = function () I didn't write "var".

This code will work perfectly, in console you will see "test".

Now check following code:

var Foo=function () {
	var number = 1;
	self.number = 1;
	return {inc: function() {
		self.number = self.number + 1;
	}}
}

Foo.prototype.bar = function () {
	console.log("test");
};

var foo = new Foo();

foo.bar();

This code will not work, we will receive error "Uncaught TypeError: undefined is not a function", because with return we will loose "this". Solution is to add return this:

var Foo=function () {
	var number = 1;
	self.number = 1;
	return this;
}

Foo.prototype.bar = function () {
	console.log("test");
};

var foo = new Foo();

foo.bar();

Private and public methods

Details
Written by: Stanko Milosev
Category: JavaScript
Published: 14 January 2015
Last Updated: 14 January 2015
Hits: 3908

Just one simple example about private and public methods in JavaScript.

Check this code in JS:

(function (ns) {
	function myCreator () {
		var self=this;
		
		function privateMethod() {
			alert("Hi I am private method and I can be executed only within myCreator class (I will be never called)");
		}
		
		self.publicMethod = function () {
			document.getElementById("publicMethodCall").innerHTML = "Hi, I am called from publicMethod";
		}
		
	}
	ns.myCreator = myCreator;
}(window.privateAndPublic))

Here you can see how private method is declared:

function privateMethod

and public method:

self.publicMethod

When you open console and check what newly created object has, you will see that there is no privateMethod (because it is inaccessible):

Example download from here.

Another way of introducing methods

Details
Written by: Stanko Milosev
Category: JavaScript
Published: 14 January 2015
Last Updated: 06 April 2022
Hits: 3925

Here and here I already explained some ways of introducing methods in javascript, and now i will show one more way.

Let's say we have JS like:

(function (ns) {
	function myCreator () {
		var self=this;
		
		function privateMethod() {
			alert("Hi I am private method and I can be executed only within myCreator class (I will never be called)");
		}
		
		self.publicMethod = function () {
			document.getElementById("publicMethodCall").innerHTML = "Hi, I am called from publicMethod";
		}
		
	}
	ns.myCreator = myCreator;
}(window.privateAndPublic))

Then, we introduce JS like:

(function (ns) {

	function myCreatorReturn() {
		var self = this;
		self.myCreator = new window.privateAndPublic.myCreator();
		
		return {
			myCreator: self.myCreator
		}
	}

	ns.myCreatorReturn = myCreatorReturn;
	
}(window.myReturn))

Here notice line:

self.myCreator = new window.privateAndPublic.myCreator();

Now we can call method "publicMethod" like:

window.onload = function() {
	mCR = new window.myReturn.myCreatorReturn();

	mCR.myCreator.publicMethod();
}

Notice line:

mCR.myCreator.publicMethod();

Example download from here.

  1. Currying (partially applying functions)
  2. Namespaces second part
  3. Change caption of button
  4. Simple animation

Subcategories

C#

Azure

ASP.NET

JavaScript

Software Development Philosophy

MS SQL

IBM WebSphere MQ

MySQL

Joomla

Delphi

PHP

Windows

Life

Lazarus

Downloads

Android

CSS

Chrome

HTML

Linux

Eclipse

Page 57 of 164

  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61