Home  >  Article  >  Web Front-end  >  Summary of commonly used native js custom functions

Summary of commonly used native js custom functions

高洛峰
高洛峰Original
2016-12-06 13:36:541390browse

js gets the date function

   
//获取当前时间日期
function CurentTime()
{
  var now = new Date();
  var year = now.getFullYear();    //年
  var month = now.getMonth() + 1;   //月
  var day = now.getDate();      //日
  var hh = now.getHours();      //时
  var mm = now.getMinutes();     //分
  var clock = year + "-";
 
  if(month < 10)
    clock += "0";
 
  clock += month + "-";
 
  if(day < 10)
    clock += "0";
 
  clock += day + " ";
 
  if(hh < 10)
    clock += "0";
 
  clock += hh + ":";
  if (mm < 10) clock += &#39;0&#39;;
  clock += mm;
  return(clock);
}

js gets the time difference function

//获取时间差多少天
function getLastTime()
  {
    var startTime=new Date("1996-5-11 00:00"); //开始时间
    var endTime=new Date();  //结束时间
    var lastTime=endTime.getTime()-startTime.getTime() //时间差的毫秒数
 
    //计算出相差天数
    var days=Math.floor(lastTime/(24*3600*1000))
 
    //计算出小时数
    var leave1=lastTime%(24*3600*1000)  //计算天数后剩余的毫秒数
    var hours=Math.floor(leave1/(3600*1000))
    //计算相差分钟数
    var leave2=leave1%(3600*1000)    //计算小时数后剩余的毫秒数
    var minutes=Math.floor(leave2/(60*1000))
 
    //计算相差秒数
    var leave3=leave2%(60*1000)   //计算分钟数后剩余的毫秒数
    var seconds=Math.round(leave3/1000)
 
    return " 相差 "+days+"天 "+hours+"小时 "+minutes+" 分钟"+seconds+" 秒";
  }

js only automatically refreshes the page once

//自动刷新页面一次后停止刷新
window.onload = function(){
  if(location.search.indexOf("?")==-1){
   location.href += "?myurl";
  }
  else{
   if(location.search.indexOf("myurl")==-1) location.href += "&myurl";
  }
}

ajax instance

$.ajax({
    type: "POST",
    url: "join.php",
    data: dataString,
    success: function(){
      $(&#39;.success&#39;).fadeIn(200).show();
      $(&#39;.error&#39;).fadeOut(200).hide();
    }
  });

Get the window size in real time

$(window).resize(function(){
  var Height = $(window).height();
  var Width = $(window).width();
})

js loop execution function and timing execution function

//循环执行,每隔3秒钟执行一次showalert()
  window.setInterval(showalert, 3000);
  function showalert()
  {
    alert("循环执行");
  }
  //定时执行,5秒后执行show()
  window.setTimeout(show,5000);
   function show()
   {
 
    alert("定时执行");
   }

js get get parameter function

function GetQueryString(name)
{
   var reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)");
   var r = window.location.search.substr(1).match(reg);
   if(r!=null)return unescape(r[2]); return null;
}
alert(GetQueryString("参数名1"));

js page printing array function

/**
 * 打印数组
 * @param {[type]} arr  要打印的数组
 * @param {[type]} space 控制打印的缩进
 * @param {[type]} space2 控制打印的缩进2
 */
 function print_arr(arr, space, space2)
 {
 
 space = space || &#39; &#39;;
 
 space2 = space2 || &#39;     &#39;;
 
 var str = "Array<br>"+space+"(<br>";
 
 for(var i=0; i<arr.length; i++)
 
 {
 
  if( Object.prototype.toString.call(arr[i]) == &#39;[object Array]&#39; )
 
  { //判断是否是数组,如果是,进行递归拼接
 
   str += space2 + &#39;[&#39; +i+"] => "+ print_arr(arr[i], space+&#39;   &#39;, space2+&#39;   &#39;);
 
  }
 
  else
 
  {
 
   str += space2 +&#39;[&#39;+i+"] => "+ arr[i] +"<br>";
 
  }
 
 }
 
 str += space+")<br>";
 
 document.write(str);
 
}

js prints json data into Array form output in html

/** 输出空格函数 */
function blank(num) {
 var res = &#39;&#39;;
 for (var i = 0; i < num; i++) {
  res += &#39; &#39;;
 }
 return res;
} 
 
/** 计算JSON对象数据个数 */
function jsonLen(jsonObj) {
 var length = 0;
 for (var item in jsonObj) {
    length++;
 }
 return length;
}
 
/** 解析JSON对象函数 */
function printObj(obj) {
 // JSON对象层级深度
 deep = (typeof(deep)==&#39;undefined&#39;) ? 0: deep;
 var html = "Array\n"; // 返回的HTML
 html += kong(deep) + "(\n";
 var i = 0;
 // JSON对象,不能使用.length获取数据的个数,故需自定义一个计算函数
 var len = typeof(obj) == &#39;array&#39; ? obj.length : jsonLen(obj);
 for(var key in obj){
  // 判断数据类型,如果是数组或对象,则进行递归
  // 判断object类型时,&&jsonLen(obj[key])是由于
  // 1、值(类似:email:)为null的时候,typeof(obj[key])会把这个key当做object类型
  // 2、值为null的来源是,数据库表中某些字段没有数据,查询之后直接转为JSON返回过来
  if(typeof(obj[key])==&#39;array&#39;|| (typeof(obj[key])==&#39;object&#39; && jsonLen(obj[key]) > 0) ){
   deep += 3;
   html += kong(deep) + &#39;[&#39; + key + &#39;] => &#39;;
   // 递归调用本函数
   html += printObj(obj[key],deep);
   deep -= 3;
  }else{
   html += kong(deep + 3) + &#39;[&#39; + key + &#39;] => &#39; + obj[key] + &#39;\n&#39;;
  }
  if (i == len -1) {
   html += kong(deep) + ")\n";
  };
  i++;
 }
 return html;
}
 
/** 向HTML页面追加打印JSON数据 */
function p_Obj(obj) {
 var div = document.getElementById(&#39;print-json-html&#39;);
 if (div != null) {
  document.body.removeChild(div);
 };
 var node = document.createElement("div");//创建一个div标签
 node.id = &#39;print-json-html&#39;;
 node.innerHTML = &#39;<pre class="brush:php;toolbar:false">&#39; + printObj(obj) + &#39;
'; document.body.appendChild(node); }

js prints the array length function of multi-dimensional array

//获取多维数组的数量
  function getArrNum(arr) {
 
    var eleNum = 0;
 
    if (arr == null) {
 
      return 0;
 
    }
 
    for (var i = 0; i < arr.length; i++) {
 
      for (var j = 0; j < arr[i].length; j++) {
 
        eleNum++;
 
      }
 
    }
 
    document.write(eleNum);
 
  }


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn