Skip to content

Instantly share code, notes, and snippets.

@nhtua
Last active November 27, 2015 04:38
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 nhtua/270289a2d5d58ab505f7 to your computer and use it in GitHub Desktop.
Save nhtua/270289a2d5d58ab505f7 to your computer and use it in GitHub Desktop.
[javascript] The common functions for js
/**
* This function check an element is scrolled into view area on screen.
* @param elem; use CSS Selector syntax to specific an element.
* @return boolean;
* @author Scott Dowding
* @refer http://stackoverflow.com/questions/487073
*/
function isScrolledIntoView(elem)
{
var $elem = $(elem);
var $window = $(window);
var docViewTop = $window.scrollTop();
var docViewBottom = docViewTop + $window.height();
var elemTop = $elem.offset().top;
var elemBottom = elemTop + $elem.height();
return ((elemBottom <= docViewBottom) && (elemTop >= docViewTop));
}
/**
* Generate random string.
* @param Integer len; Length of string
* @return String;
* @author Roger Knapp
* @refer http://stackoverflow.com/a/1349426/1235074
*/
function random(len)
{
var text = "";
var possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
for( var i=0; i < len; i++ )
text += possible.charAt(Math.floor(Math.random() * possible.length));
return text;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment