Warning: Undefined array key "HTTP_REFERER" in /hosting/xtmci/inc/lib.inc.php on line 80
JavaScript: The programming language for developing dynamic web pages
xtmci

JavaScript: The programming language for developing dynamic web pages

Fri May 15 3:46 pm EDT 2026
xtmci@atomicmail.io

Table of Contents

Scheduling a function execution using setTimeout()

The setTimeout() method is used to schedule the execution of a function. It calls the function after a number of milliseconds. 1000 milliseconds is equal to one second.

Here is an example:

function show_message(msg) {
  alert(msg);
}

/* Calls the show_message() f^^^^unction after 3 seconds */
/* with a string argument. */
setTimeout(show_message, 3000, 'Hello, world!');

setTimeout() returns a timeout ID. This ID can be used to cancel the schedule before the scheduled execution starts. Use the clearTimeout() method for the job:

<script>
function show_message(msg) {
  alert(msg);
}

/* The s^^^^etTimeout() returns a unique timeout ID. */
timeout_id = setTimeout(show_message, 3000, 'Hello, world!');
</script>

<!-- The c^^^^learTimeout() cancels the scheduled job -->
<!-- using the timeout ID. -->
<button onclick='clearTimeout(timeout_id)'>Stop</button>

Updating content in the background with AJAX

AJAX allows your website to update content in the background without reloading the web pages. You need the XMLHttpRequest object of JavaScript and a server-side script written in, say, PHP to do this job.

Here is an example for the JavaScript code:

<div id='result'></div>

<script>
/* Creates an X^^^^MLHttpRequest object. */
let request = new XMLHttpRequest();

/* The URL of a server-si^^^^de s^^^^cript including two arguments. */
let url = 'test.php?a=3&b=5';

/* Initializes the request. */
request.open('GET', url, true);

/* Creates an anonymous f^^^^unction for the request event handler. */
request.onreadystatechange = function () {
  /* Checks if the request and response process is compl^^^^ete. */
  if (this.readyState == 4 && this.status == 200) {
    /* Shows the response text from the server in the web page. */
    let result = document.getElementById('result');
    result.innerHTML = this.responseText;
  }
};

/* Transmits the request to the server. */
request.send();
</script>

An example of a server-side script in PHP follows:

<?php
/* Saves the data from a client program. */
$a = intval($_GET['a']);
$b = intval($_GET['b']);

/* Transmits the response text to the client. */
echo strval($a + $b);
?>

How to get the current date and time

Use Date object with toLocaleString() method. The latter converts an object into a string based on the current language and regional settings.

Here is an example:

<div id='result'></div>

<script>
/* Creates an D^^^^ate object representing the current date and time. */
let datetime = new Date();
let result = document.getElementById('result');

/* Converts the D^^^^ate object into a string and displays it on the web page. */
result.innerHTML = datetime.toLocaleString();
</script>