-- Practice Final Sec3
//Group Members: Avinash More, Kevin Hou
7. Give an application code example of each of the following in Javascript: (a) timer, (b) XHR request, (c) Promise.
Answer:
(a) timer:
The two key methods to use with JavaScript are:
setTimeout(function, milliseconds)
- Executes a function, after waiting a specified number of milliseconds.
setInterval(function, milliseconds)
- Same as setTimeout(), but repeats the execution of the function continuously.
//Made a button that alerts hello 3 seconds after the click
<button onclick="setTimeout(myFunction, 3000)">Try it</button>
<script>
function myFunction() {
alert('Hello');
}
</script>
(b)
To create an XMLHttpRequest one could simply write in Javascript:
request = new XMLHttpRequest();
If we need to set up HTTP request headers we can use:
request.setRequestHeader("name", "value");
Sample Application code:
//make a XHR request in JS and handle the readyState
//create a http request
request = new XMLHttpRequest();
//callback function to select the case based on the response we get back
request.onreadystatechange = function()
{
switch(request.readyState) {
/*
case 0:// handle uninitialized case
case 1: // handle open but no send case
case 2: // handle send but no response case
case 3: // handle response is being downloaded case
*/
case 4:
document.getElementById("test-news").innerHTML = request.responseText;
break;
}
}
//make the request
request.open("GET", "test_news.html", true);
request.send();
(c) Promise:
var my_promise = new Promise(function(fulfill, reject) {
//get the random number
var n = Math.floor(Math.random() * 20);
//
if (n === 6) {
fulfill(n);
} else {
reject(n);
}
});
my_promise.then(function(n) {
console.log('Random number was '+n);
}. catch(n) {
console.log('Random number wasn't 6 because it was '+n);
});
//Group Members: Avinash More, Kevin Hou
7. Give an application code example of each of the following in Javascript: (a) timer, (b) XHR request, (c) Promise.
Answer:
(a) timer:
The two key methods to use with JavaScript are:
setTimeout(function, milliseconds)
- Executes a function, after waiting a specified number of milliseconds.
setInterval(function, milliseconds)
- Same as setTimeout(), but repeats the execution of the function continuously.
//Made a button that alerts hello 3 seconds after the click
<button onclick="setTimeout(myFunction, 3000)">Try it</button>
<script>
function myFunction() {
alert('Hello');
}
</script>
(b)
To create an XMLHttpRequest one could simply write in Javascript:
request = new XMLHttpRequest();
If we need to set up HTTP request headers we can use:
request.setRequestHeader("name", "value");
Sample Application code:
//make a XHR request in JS and handle the readyState
//create a http request
request = new XMLHttpRequest();
//callback function to select the case based on the response we get back
request.onreadystatechange = function()
{
switch(request.readyState) {
/*
case 0:// handle uninitialized case
case 1: // handle open but no send case
case 2: // handle send but no response case
case 3: // handle response is being downloaded case
*/
case 4:
document.getElementById("test-news").innerHTML = request.responseText;
break;
}
}
//make the request
request.open("GET", "test_news.html", true);
request.send();
(c) Promise:
var my_promise = new Promise(function(fulfill, reject) {
//get the random number
var n = Math.floor(Math.random() * 20);
//
if (n === 6) {
fulfill(n);
} else {
reject(n);
}
});
my_promise.then(function(n) {
console.log('Random number was '+n);
}. catch(n) {
console.log('Random number wasn't 6 because it was '+n);
});