forked from mozilla/wsoh
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsong-search.html
59 lines (55 loc) · 1.53 KB
/
song-search.html
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
<html>
<head>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js"></script>
<style>
.song-list-item {
padding: 10px;
margin: 10px;
font-size: 12px;
background-color: red;
}
</style>
<script>
$(document).ready ( function () {
var timer,
$searchResults = $('#search-results'),
$searchBox = $('#song-search-box');
$searchBox.keyup(function () {
clearTimeout(timer);
timer = setTimeout(function () {
var search = $searchBox[0].value;
$searchResults.fadeOut(10).html('').fadeIn(10);
$.('http://localhost:4567/search?q='+search, function (response) {
response = $.parseJSON(response);
var add=[];
try {
for (var i=0,len=200;i<len;i++) { //iterates through each item of response
//benchmark: rendering all 200 items only take 5 ms or so. slow part is ajax request
song = response[i];
//track = song.track;
//artistID = song.artistID;
songName = song.songName;
//albumID = song.albumID;
//albumName = song.albumName;
//artworkUrl = song.artworkUrl;
artistName = song.artistName;
//songID = song.songID
add.push('<div class="song-list-item">' + songName + ' by ' + artistName + '</div>');
}
$searchResults.html(add.join(""));
} catch (e) {
$searchResults.html("No results")
}
})
$searchResults.html("Searching");
}, 300);
})
})
</script>
</head>
<body>
<input id="song-search-box" placeholder="Add a song!"></input>
<div id="search-results">
</div>
</body>
</html>