An easy to use type feature that uses the placeholder attribute and setTimer to give the effect of a user typing. Helpful if you need to draw attention to the search box field.
I'm looking to know if this type of implementation would be a performance impact or cause browser lag. Help in making this more condensed would also be helpful. I was thinking about using $.extend, but leaned away. Any reason why I shouldn't make this a jquery plugin?
https://jsfiddle.net/googabeast/jsepy3ed/
obj = {
searchTxt: [
"Welcome. What are you searching for? ",
"Something else to type out "
"Note the space at the end -> "
],
hasFocus:false
}
if(obj.searchTxt !== null && obj.searchTxt !== "undefined"){
var i = 0, ct = 0; obj.typeIt;
function type(){
obj.typeIt = obj.searchTxt[ct].slice(0, ++i);
if(obj.typeIt === obj.searchTxt[ct]){
i = 0;
if(ct === obj.searchTxt.length-1){
ct = 0;
}else{
++ct;
}
return;
};
$(".searchBox").attr("placeholder", obj.typeIt);
setTimeout(type, 80);
};
type();
$(window).on({
focus: function(){ obj.hasFocus = true; },
blur: function(){ obj.hasFocus = false; }
}).trigger("focus");
setInterval(function(){
if(obj.hasFocus){
type();
};
}, 10000);
};