Skip to content

Instantly share code, notes, and snippets.

@iankit3
Last active March 2, 2020 15:42
Show Gist options
  • Save iankit3/c8b7b0ac145c28fe8af5c1641091753c to your computer and use it in GitHub Desktop.
Save iankit3/c8b7b0ac145c28fe8af5c1641091753c to your computer and use it in GitHub Desktop.
Autocomple using Debounce or Throttle - JS Bin// source https://jsbin.com/fajikaj
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>JS Bin</title>
</head>
<body>
<script src="https://unpkg.com/@reactivex/rxjs@5.0.3/dist/global/Rx.js"></script>
<div>
<input type="text" id="search" placeholder="Search or enter to add">
</div>
<div>
<ul id="ul">
</ul>
</div>
<script id="jsbin-javascript">
'use strict';
var res = { data: [{ name: 'The Meyerowitz Stories (New and Selected)' }, { name: 'Coco' }, { name: 'Logan' }, { name: "Wind River " }] };
var ul = document.getElementById('ul');
var search = document.getElementById('search');
renderList("");
function renderList(value) {
console.log(value);
ul.innerHTML = "";
var d = processList(value).forEach(function (el) {
var node = document.createElement("LI");
var textnode = document.createTextNode(el.name);
node.appendChild(textnode);
ul.append(node);
});
}
function processList(value) {
return res.data.filter(function (el) {
return el.name.toLowerCase().includes(value.toLowerCase());
});
}
function addToList(val) {
res.data.push({ name: val });
}
var input = Rx.Observable.fromEvent(search, 'keyup').map(function (ev) {
if (ev.keyCode == 13) {
addToList(ev.target.value);
ev.target.value = '';
}
return ev.target.value;
})
//.throttle(ev => Rx.Observable.interval(300))
.debounce(function (ev) {
return Rx.Observable.interval(300);
});
// Both can be use
// Throttle is called on the start of interval
// But
// Debounce is called at the end o interval
var count = 0;
input.subscribe(function (value) {
console.log(count++);
renderList(value);
});
</script>
<script id="jsbin-source-javascript" type="text/javascript"> var res = {data:[{name: 'The Meyerowitz Stories (New and Selected)'} ,{name : 'Coco'} , {name: 'Logan'} , {name :"Wind River "}]};
var ul = document.getElementById('ul');
var search = document.getElementById('search');
renderList("")
function renderList(value){
console.log(value)
ul.innerHTML = "";
var d = processList(value)
.forEach(el => {
var node = document.createElement("LI");
var textnode = document.createTextNode(el.name);
node.appendChild(textnode);
ul.append(node);
})
}
function processList(value){
return res.data.filter( el => {
return el.name.toLowerCase().includes(value.toLowerCase());
})
}
function addToList(val){
res.data.push({name:val});
}
var input = Rx.Observable.fromEvent(search, 'keyup')
.map(ev => {
if(ev.keyCode == 13) {
addToList(ev.target.value) ;
ev.target.value = '';
}
return ev.target.value
})
//.throttle(ev => Rx.Observable.interval(300))
.debounce(ev => Rx.Observable.interval(300))
// Both can be use
// Throttle is called on the start of interval
// But
// Debounce is called at the end o interval
var count = 0;
input.subscribe((value) => {
console.log(count++)
renderList(value);
})</script></body>
</html>
'use strict';
var res = { data: [{ name: 'The Meyerowitz Stories (New and Selected)' }, { name: 'Coco' }, { name: 'Logan' }, { name: "Wind River " }] };
var ul = document.getElementById('ul');
var search = document.getElementById('search');
renderList("");
function renderList(value) {
console.log(value);
ul.innerHTML = "";
var d = processList(value).forEach(function (el) {
var node = document.createElement("LI");
var textnode = document.createTextNode(el.name);
node.appendChild(textnode);
ul.append(node);
});
}
function processList(value) {
return res.data.filter(function (el) {
return el.name.toLowerCase().includes(value.toLowerCase());
});
}
function addToList(val) {
res.data.push({ name: val });
}
var input = Rx.Observable.fromEvent(search, 'keyup').map(function (ev) {
if (ev.keyCode == 13) {
addToList(ev.target.value);
ev.target.value = '';
}
return ev.target.value;
})
//.throttle(ev => Rx.Observable.interval(300))
.debounce(function (ev) {
return Rx.Observable.interval(300);
});
// Both can be use
// Throttle is called on the start of interval
// But
// Debounce is called at the end o interval
var count = 0;
input.subscribe(function (value) {
console.log(count++);
renderList(value);
});
@iankit3
Copy link
Author

iankit3 commented Mar 2, 2020

// Both can be used
// Throttle is called on the start of interval
// But
// Debounce is called at the end o interval

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment