-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
85 lines (68 loc) · 1.56 KB
/
Copy pathscript.js
File metadata and controls
85 lines (68 loc) · 1.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
var words = (function(){
var words = [
'hello',
'hola',
'ciao',
'konnichiwa',
'guten tag',
'oi',
'shalom',
'salut',
'namaskar',
'yassou',
'hallo',
'dobro jutro',
'nǐ hǎo',
'ألسّلام عليكم'
],
el = document.querySelector('.hello'),
currentIndex,
currentWord,
prevWord,
duration = 3000;
var _getIndex = function(max, min){
currentIndex = Math.floor(Math.random() * (max - min + 1)) + min;
//Generates a random number between beginning and end of words array
return currentIndex;
};
var _getWord = function(index){
currentWord = words[index];
return currentWord;
};
var _clear = function() {
setTimeout(function(){
el.className = 'hello';
}, duration/4);
};
var _toggleWord = function(duration){
setInterval(function(){
//Stores value of previous word
prevWord = currentWord;
//Generate new current word
currentWord = words[_getIndex(words.length-1, 0)];
//Generate new word if prev matches current
if(prevWord === currentWord){
currentWord = words[_getIndex(words.length-1, 0)];
}
//Swap new value
el.innerHTML = currentWord;
//Clear class styles
_clear();
//Fade in word
el.classList.add(
'js-block',
'js-fade-in-hello'
);
}, duration);
};
var _init = function(){
_toggleWord(duration);
};
//Public API
return {
init : function(){
_init();
}
};
})();
words.init();