From 87a1642232676a74d34556e73f812e648ab067aa Mon Sep 17 00:00:00 2001 From: Valentin Popov Date: Thu, 31 May 2018 08:10:16 +0400 Subject: Refactoring the script Signed-off-by: Valentin Popov --- assets/javascript/script.js | 98 ++++++++++++++++++++++++--------------------- 1 file changed, 52 insertions(+), 46 deletions(-) (limited to 'assets') diff --git a/assets/javascript/script.js b/assets/javascript/script.js index 1055a3e..ac59acb 100644 --- a/assets/javascript/script.js +++ b/assets/javascript/script.js @@ -1,5 +1,46 @@ 'use strict'; +var client = new XMLHttpRequest(); +var radio = findGetParameter('radio'); +var url = '//somafm.com/songs/' + radio + '.xml'; + +/* Processes response */ +client.onload = function () { + if (client.readyState === client.DONE) { + if (this.status === 200 && this.responseXML !== null) { + /* Gets current track */ + var current = this.responseXML.getElementsByTagName('song')[0]; + var artistTrack = current.getElementsByTagName('artist')[0].textContent; + var titleTrack = current.getElementsByTagName('title')[0].textContent; + + /* Gets items on the page */ + var displayElement = document.getElementById('display'); + var artistElement = document.getElementById('artist'); + var titleElement = document.getElementById('title'); + + if (artistElement.textContent !== artistTrack || titleElement.textContent !== titleTrack) { + /* Updates text */ + artistElement.textContent = artistTrack; + titleElement.textContent = titleTrack; + + /* Displays a pop-up window */ + displayElement.style['animation-name'] = 'fadeIn'; + + setTimeout(function () { + /* Removes a pop-up window */ + displayElement.style['animation-name'] = 'fadeOut'; + }, getInterval('duration')); + } + } + } +}; + +/* Update cycle */ +setInterval(function () { + client.open('GET', url); + client.send(); +}, getInterval('interval')); + /** * @param {String} parameterName - Variable name * @returns {String} Value of variable @@ -21,53 +62,18 @@ function findGetParameter(parameterName) { } /** - * @param {String} artistTrack - Artist of the track - * @param {String} titleTrack - Name of the track - * @description Updates the data on the page. Displays a pop-up window if the - * data has changed. + * @param {String} parameterName - Variable name + * @returns {Number} Timer value, default 10 seconds + * @description Gets the settings for the specified timer. */ -function updateData(artistTrack, titleTrack) { - /* Gets items on the page */ - var displayElement = document.getElementById('display'); - var artistElement = document.getElementById('artist'); - var titleElement = document.getElementById('title'); +function getInterval(parameterName) { + var value = findGetParameter(parameterName); + var interval = parseInt(value, 10); + var result = 10000; - if (artistElement.textContent !== artistTrack || titleElement.textContent !== titleTrack) { - /* Updates text */ - artistElement.textContent = artistTrack; - titleElement.textContent = titleTrack; - - /* Displays a pop-up window */ - displayElement.style['animation-name'] = 'fadeIn'; - - setTimeout(function () { - /* Removes a pop-up window */ - displayElement.style['animation-name'] = 'fadeOut'; - }, 10000); - } -} - -var client = new XMLHttpRequest(); -var radio = findGetParameter('radio'); -var url = '//somafm.com/songs/' + radio + '.xml'; - -/* Processes response */ -client.onload = function () { - if (client.readyState === client.DONE) { - if (this.status === 200 && this.responseXML !== null) { - /* Gets current track */ - var current = this.responseXML.getElementsByTagName('song')[0]; - var artist = current.getElementsByTagName('artist')[0].textContent; - var title = current.getElementsByTagName('title')[0].textContent; - - /* Updates data */ - updateData(artist, title); - } + if (isNaN(interval) === false) { + result = interval; } -}; -/* Update cycle */ -setInterval(function () { - client.open('GET', url); - client.send(); -}, 15000); + return result; +} -- cgit v1.2.3