I have a text box and whenever the user types something, I want to update the contents of a span. However, I do want to wait for sometime before I update the span. I tried using setTimeout and even setInterval but it doesn't seem to work.
Here is my html:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Sample Title</title>
</head>
<body>
<input type="text" id="text-box-1" />
<span id="results"></span>
</body>
</html>
And my JS:
function update(textValue){
document.querySelector("#results").innerHTML = textValue;
}
document.querySelector("#text-box-1").onkeyup = function(e){
setInterval(update(document.querySelector("#text-box-1").value), 5000);
}
When I type in the textbox it starts showing up inside the span instead of waiting for 5 secs. What am I missing here?
Thanks.