function decrypt() {
var word = "TEST";
for (var n = 0; n <= 3; n++) {
console.log("n = " + n);
for (var i = 1; i <= 9; i++) {
(function(i) {
setTimeout(function() { // delay each iteration...for this test, every 3 seconds, slow enough to see the change and see what problems exist
// Get Letter (A, B, C, etc.)
console.log(n);
console.log(i);
console.log("letter" + n);
var letter = document.getElementById("letter" + n).innerHTML;
// Increment font number, i.e., font0 to font1, to font2, etc.
document.getElementById("letter" + n).className = "font" + i;
// If less than 10 increments, keep incrementing letter, A, B, C, D, etc.
if (i != 9) {
letter = String.fromCharCode(word.charCodeAt() + 1);
}
// If the final increment, return the next letter of the final word string
else {
letter = word.charAt(n);
}
// Change the letter displayed in the DIV
document.getElementById("letter" + n).innerHTML = letter;
}, 3000 * i);
})(i);
}
}
}
@import url('https://fonts.googleapis.com/css?family=Lato|Unlock|Raleway');
.font0 {
font-family: 'Lato', sans-serif;
}
.font1 {
font-family: 'Unlock', sans-serif;
}
.font2 {
font-family: 'Raleway', sans-serif;
}
.font3 {
font-family: 'Lato', sans-serif;
}
.font4 {
font-family: 'Unlock', sans-serif;
}
.font5 {
font-family: 'Raleway', sans-serif;
}
.font6 {
font-family: 'Lato', sans-serif;
}
.font7 {
font-family: 'Unlock', sans-serif;
}
.font8 {
font-family: 'Raleway', sans-serif;
}
.font9 {
font-family: 'Lato', sans-serif;
}
<div id="letter0" class="font0">A</div>
<div id="letter1" class="font0">A</div>
<div id="letter2" class="font0">A</div>
<div id="letter3" class="font0">A</div>
<br /><br />
<button onclick="decrypt()">Click me</button>
That's my code. I have two loops. Nested.
It doesn't work.
Looking at my console, as I tried to debug it:
n = 0
footer.php:25 n = 1
footer.php:25 n = 2
footer.php:25 n = 3
footer.php:31 4
footer.php:32 1
footer.php:33 letter4
footer.php:34 Uncaught TypeError: Cannot read property 'innerHTML' of null at footer.php:34
(anonymous) @ footer.php:34
setTimeout (async)
(anonymous) @ footer.php:29
decrypt @ footer.php:44
onclick @ footer.php:49
footer.php:31 4
footer.php:32 1
footer.php:33 letter4
I'm not understanding what is happening. The first for loop should start n = 0
, go into the second for loop, i = 0
through i = 9
, back to the first loop, n = 1
, back into the second for another round of i = 0
through i = 9
, etc.
It loops through the first for
, n = 0
through n = 4
, then loops through the second for i = 0
through i = 9
.
And since it does that, I cannot test out the rest of the code, as there is no HTML element with an id
of letter4
.
And since it'll probably come up: what I'm wanting to do is use different fonts, eventually different symbol fonts, and do a "decryption" style effect like https://medium.com/chingu/decrypting-effect-c5fc2a071354 ...but slightly different, symbols instead of standard letters and numbers, and being straightforward with what's going on. And, going one letter at a time, T first, E second, S third, T fourth, resulting in "TEST"
It should be noted: I work primarily with PHP, and so I've written the JavaScript mostly the same way I would have written it in PHP, and since I cannot test it out AT ALL, I don't know what other problems there may be (so if you should see something glaring, please let me know)