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

Example of a Javascript Closure: setTimeout Inside a For Loop

Details
Written by: Stanko Milosev
Category: JavaScript
Published: 11 April 2016
Last Updated: 11 April 2016
Hits: 5169

Title I have stolen from here.

Here is my example. This example will return five times five:

for (var i=0; i < 5; i++) {
	setTimeout(function () {
    console.log(i)
  }, i*1000)
}

And this example will return 0, 1, 2, 3, 4:

var aa = function (a) {setTimeout(function () {
    console.log(a)
  }, i*1000)};
  
for (var i=0; i < 5; i++) {
	aa(i);
}

 

idangero.us swiper 2.4.2 example

Details
Written by: Stanko Milosev
Category: JavaScript
Published: 02 February 2016
Last Updated: 02 February 2016
Hits: 5097

One example of swiping with old version of swiper.

HTML:

<div class="swiper-container">
	<div class="swiper-wrapper">
		<div class="swiper-slide">
			Test slide one
		</div>
		<div class="swiper-slide">
			Test slide two
		</div>
		<div class="swiper-slide">
			Test slide three
		</div>
	</div>
</div>

JS:

$('.swiper-container').swiper({
    grabCursor: true
});

Live example you can see here.

Promises

Details
Written by: Stanko Milosev
Category: JavaScript
Published: 06 November 2015
Last Updated: 09 November 2015
Hits: 4943

HTML:

<!DOCTYPE html>
<html lang="en">
<head>
    <script type="text/javascript" src="/jquery-2.1.4.js"></script>
    <script type="text/javascript" src="/index.js" defer></script>
    <meta charset="UTF-8">
    <title></title>
</head>
<body>

    <div id="withoutPromise">
        <b>Example without promise:<p/></b>
    </div>

    <div id="withJqueryPromise">
        <b>Example with jQuery promise:<p/></b>
    </div>

    <div id="pureJavaScriptPromiseWithoutJquery">
        <b>Example with pure JavaScript promise (without jQuery):<p/></b>
    </div>

</body>
</html>

JS:

/*global console, $, XMLHttpRequest*/
(function () {
    "use strict";

    function fillResult(id, data) {
        $.each(data, function (key, val) {
            $(id).append("key: " + key + "<br/>value: " + val + "<p/>");
        });
    }

    function fillPromiseResult(data) {
        $.each(JSON.parse(data), function (key, val) {
            $('#pureJavaScriptPromiseWithoutJquery').append("key: " + key + "<br/>value: " + val + "<p/>");
        });
    }

    $.getJSON("index.json", function (data) {
        fillResult("#withoutPromise", data);
    });

    $.getJSON("index.json").done(function (data) {
        fillResult("#withJqueryPromise", data);
    });

    function makeRequest() {
        return new Promise(function (resolve, reject) {
            var myJson = new XMLHttpRequest();
            myJson.open("GET", "http://localhost:63342/promiseExample/index.json");

            myJson.onload = function () {
                if (this.status >= 200 && this.status < 300) {
                    resolve(myJson.response);
                } else {
                    reject(this.statusText);
                }
            };

            myJson.send();

        });
    }

    makeRequest().then(fillPromiseResult, function (errorMsg) {
        $("#pureJavaScriptPromiseWithoutJquery").append(errorMsg);
    }).catch(function (err) {
        console.log("Error: " + err);
    });
}());

JSON:

{
  "first": "first test",
  "second": "second test",
  "nested test": [
    {"first nest": "nest one"},
    {"second nest": "nest two"}
  ]
}

Mostly I was using this example. Notice that it seems that resolve method of promise can accept only one parameter, that is why I needed to create function special for promise. Example download from here.

Inline if

Details
Written by: Stanko Milosev
Category: JavaScript
Published: 19 October 2015
Last Updated: 19 October 2015
Hits: 4933

Example of inline if in javascript:

var a = 30;
var b = 40; v
ar c = ((a < b) ? 2 : 3);

Copy / pasted from here.

Example of inline checking of null:

var whatIWant = someString || "Cookies!";

Copy / pasted from here.

  1. Stroke style
  2. Getters and setters
  3. Few things which I learned
  4. Reading EXIF meta data from JPEG image files

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 55 of 164

  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59