Skip to content

Instantly share code, notes, and snippets.

@levelsio
Created April 18, 2017 18:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save levelsio/e8113564b8ac2634393b4bfebf54feaf to your computer and use it in GitHub Desktop.
Save levelsio/e8113564b8ac2634393b4bfebf54feaf to your computer and use it in GitHub Desktop.
/* <type text simulation> */
var currentSampleJob=sampleJobs[0];
var autoTypingActive=true;
var transitionDelayTime=0;
var sampleJobLength=0;
var typingNow=false;
var sampleJobTimer=0;
var timer=null;
var searchElement = $('.search');
/* <blinking cursor> */
setInterval(function() {
if(autoTypingActive) {
if(searchElement.val().substr(-1)=='|') {
searchElement.val(searchElement.val().substr(0,searchElement.val().length-1)+' ');
}
else if($('.search').val().substr(-1)==' ') {
searchElement.val(searchElement.val().substr(0,searchElement.val().length-1)+'|');
}
else {
searchElement.val(searchElement.val()+'|');
}
}
},500);
/* </blinking cursor> */
/* <let user disable simulator> */
var prevValue;
searchElement.bind('focus',function() {
if(autoTypingActive) {
$(this).val('');
clearTimeout(timer);
autoTypingActive=false;
}
});
searchElement.bind('click keyup',function() {
if(autoTypingActive) {
$(this).val('');
clearTimeout(timer);
autoTypingActive=false;
}
if(prevValue!=$(this).val()) {
//$(this).val(ucfirst($(this).val()));
prevValue=$(this).val();
}
});
searchElement.bind('blur',function(e) {
if($(this).val()=='') {
sampleJobLength=0;
typingNow=false;
autoTypingActive=true;
nextSampleJob(true);
}
});
/* </let user disable simulator> */
/* <typeText> */
typeText=function() {
displaySampleJob=currentSampleJob.substr(0, sampleJobLength++);
if(empty(displaySampleJob)) {
searchElement.val(' ');
}
else {
searchElement.val(displaySampleJob);
//searchElement.focus();
}
if(sampleJobLength < currentSampleJob.length+1) {
// next letter
typingNow=true;
randomMultiplier=80;
random=Math.floor(Math.random()*(randomMultiplier*2));
timer=setTimeout(typeText, random);
} else {
// start backspacing
sampleJobLength = 0;
currentSampleJob = '';
typingNow=false;
timer=setTimeout(backspaceText,1250+250*Math.random());
}
}
/* </typeText> */
/* <backspaceText> */
backspaceText=function() {
displaySampleJob=searchElement.val().slice(0, -1);
/* avoid empty div */
if(empty(displaySampleJob)) {
searchElement.val(' ');
}
else {
searchElement.val(displaySampleJob);
}
if(!empty(displaySampleJob)) {
// backspace again
randomMultiplier=80;
random=Math.floor(Math.random()*(randomMultiplier*1.5));
timer=setTimeout(backspaceText, random);
//searchElement.focus();
}
else {
// next sampleJob
nextSampleJob();
}
}
/* </backspaceText> */
/* <nextSampleJob> */
nextSampleJob=function(instantly) {
sampleJobTimer++;
// if last sampleJob, reset to first
if(sampleJobTimer>(sampleJobs.length-1)) {
sampleJobTimer=0;
}
currentSampleJob=sampleJobs[sampleJobTimer];
if(instantly) {
typeText();
}
else {
timer=setTimeout(typeText,500);
}
}
/* </nextSampleJob> */
typeText();
/* </type text simulation> */
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment