-
Notifications
You must be signed in to change notification settings - Fork 9
/
poke_memory.js
37 lines (34 loc) · 999 Bytes
/
poke_memory.js
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
$(function () {
function buildGrid(root) {
for (var i = 1; i <= 10; i++) {
for (var j = 1; j <= 10; j++) {
var cell = $('<div>').addClass('cell');
cell.attr('data-row', i).attr('data-col', j);
root.append(cell);
}
}
}
function addListeners() {
$('.cell').on('click', function (event) {
var cell = $(event.currentTarget);
var id = cell.data('row') + "" + cell.data('col');
var url = 'http://pokeapi.co/api/v2/pokemon/' + id + '/';
var interval = setInterval(function () {
cell.toggleClass('active');
}, 300)
$.ajax({
url: url,
method: 'get',
success: function (response) {
clearInterval(interval);
cell.addClass('active');
var imageUrl = response.sprites.front_default;
cell.css('background-image', 'url(' + imageUrl + ')');
}
})
})
}
var container = $('.container')
buildGrid(container);
addListeners();
});