Countdown:
<span style="font-family:SimSun;font-size:10px;">/* Implement multiple times for countdown, or format the time directly: time is a timestamp*/ var deadline = { counting:function(ele,timer){ var _this=this; setTimeout(function(){ if(timer >=0 ){ ele.html(_this.transfer(timer-1)); _this.counting(ele,timer-1); } },1000) }, transfer:function(time){ var d=Math.floor(time/86400); var h=Math.floor((time-d*86400)/3600); var m=Math.floor((time-d*86400-h*3600)/60); var s=time-d*86400-h*3600-m*60; return d+'day'+h+'hour'+m+'minute'+s+'second'; }, transferTime:function(time){/* Compare with the current time, if it is less than the current time, it will not be displayed*/ var now = new Date(), endDate = new Date(time*1000), leftTime = endDate.getTime()-now.getTime(), leftsecond = parseInt(leftTime/1000); if( leftsecond >0 ){ var date = new Date(time*1000); var year = date.getFullYear(); var month = date.getMonth()+1; month = month < 10 ? ( '0'+month ) : month; var day = date.getDate() < 10 ? ( '0'+date.getDate() ) : date.getDate(); var hour = date.getHours() < 10 ? ( '0'+date.getHours() ) : date.getHours(); var minute = date.getMinutes() < 10 ? ( '0'+date.getMinutes() ) : date.getMinutes(); var second = date.getSeconds() < 10 ? ( '0'+date.getSeconds() ) : date.getSeconds(); return year+'-'+month+'-'+day+' '+hour+':'+minute+':'+second; }else{ return ''; } } };</span>
Amount formatted in thousands:
amount.toString().replace(^/\d{1,3}(?=(\d{3})+(\.\d*)$)/g, '$&,');
js font adaptation function:
function fixRem(){ var windowWidth = window.innderWidth || document.documentElement.clientWidth || document.body.clientWidth; windowWidth = windowWidth < 320 ? 320 : windowWidth ; //body min-width in css var rootSize = 20 * ( windowWidth / 360); rootSize = rootSize> 36? 36: rootSize; var htmlNode = document.getElementsByTagName("html")[0]; htmlNode.style.fontSize = rootSize+'px'; }
css font adaptation method:
@min-font-size: 20px; @max-font-size: 36px; @min-screen: 360px; @max-screen: 800px; :root { font-size: @min-font-size; } @media (min-width:@min-screen) and (max-width:@max-screen) { :root { font-size: calc( @min-font-size + (@max-font-size - @min-font-size) * ((100vw - @min-screen) / (@max-screen - @min-screen))); } } @media (min-width:@max-screen) { :root { font-size: @max-font-size; } }
Get the json object of the form:
function getFormJson(form) { var o = {}; var a = $(form).serializeArray(); $.each(a, function() { if (!this.name) return true; if (o[this.name] !== undefined) { if (!o[this.name].push) { o[this.name] = [o[this.name]]; } o[this.name].push(this.value || ''); } else { o[this.name] = this.value || ''; } }); return o; }
Get object length
function getPropertyCount(o){ var n, count = 0; for(n in o){ if(o.hasOwnProperty(n)){ count++; } } return count; }
Object.keys(obj).length
Restrict the input box to only enter numbers (retain N decimal places), regardless of pasting
//Get the position of the current cursor in the text box function getCurPosition(domObj) { var position = 0; if (domObj.selectionStart || domObj.selectionStart == '0') { position = domObj.selectionStart; } else if (document.selection) { //for IE domObj.focus(); var currentRange = document.selection.createRange(); var workRange = currentRange.duplicate(); domObj.select(); var allRange = document.selection.createRange(); while (workRange.compareEndPoints("StartToStart", allRange) > 0) { workRange.moveStart("character", -1); position++; } currentRange.select(); } return position; } $.fn.decimalinput = function(num) { $(this).css("ime-mode", "disabled"); this.on("keypress", function(e) { if (e.charCode === 0) return true; //non-character keys for firefox var code = (e.keyCode? e.keyCode: e.which); // and Firefox IE if (code >= 48 && code <= 57) { var pos = getCurPosition(this); var dotPos = this.value.indexOf("."); if ( code == 48 && pos == 0) { return false; } if (dotPos > 0 && pos > dotPos) { if (pos > dotPos + num) return false; if (this.value.substr(dotPos + 1).length < num) return true; else return false; } return true; } //enter"." if (code == 46) { if (/^[0-9]+\.$/.test(this.value + String.fromCharCode(code))) return true; } return false; }); this.on("input propertychange", function() { var value = this.value; if( value.substr(0,1)== 0 ){ value = value.substring(1, value.length); this.value = value; } var reg = new RegExp('(?:[1-9]\d*?(?:\.\d{0,'+num+'})?)$'); if( !reg.test(value)){ this.value = value.replace(/[^0-9\.]/g, ""); } }); this.on("paste dragenter contextmenu", function() { return false; }); };
$("#property_money").decimalinput(4);Notice:
1. selectionStart does not work for number input, it seems that it only works for input type of text
2. ime-mode: css property, which sets or retrieves whether the user is allowed to activate the input method (IME) state for inputting Chinese, Korean, Japanese, etc.
refer to:
http://www.qdfuns.com/notes/26716/6ada0d47a845cc2f581bd85d28d270c9.html
http://blog.csdn.net/huangyong19870618/article/details/5873044
http://www.cnblogs.com/nackman/archive/2013/01/28/2880154.html
eval
Executes in the current scope only if called directly and the calling function is itself. eval
Otherwise, its scope is global. The use of eval is not recommended.
// call directly var foo = 1; function test() { var foo = 2; eval('foo = 3'); return foo; } test(); // 3 foo; // 1 //indirect call var foo = 1; function test() { var foo = 2; var bar = eval; bar('foo = 3'); return foo; } test(); // 2 foo; // 3
setTimeout
and
setInterval
both can accept a string as their first argument, which is used internally eval
. This string
is always
executed in the global scope, so
eval
is not called directly in this case. It is recommended to use anonymous functions for the first parameter.