1. Prevent repeated clicks in javascript and prevent excessive clicks
To prevent repeated clicks, you can add a switch, so that the switch defaults to true, and the first click will change it to false. The execution of the click event requires determining whether the switch is true. If true is executed, false will not be executed. Examples are as follows:
var isclick= true; function click(){ if(isclick){ isclick = false; //Add the events to be executed below ... }
If you just prevent clicking too fast, you can also set a timer. After a certain period of time, the switch will automatically change to true. The following example is that after 500 milliseconds, the switch will automatically change to true.
var isclick= true; function click(){ if(isclick){ isclick= false; //Add the events to be executed below ... //Timer setTimeout(function(){ isclick = true; }, 500); }
2. jquery implements 60-second countdown
Method 1:
var time = 60; //Countdownfunction getRandomCode() { if (time === 0) { time = 60; return; } else { time--; $('#time i').text(time); } setTimeout(function() { getRandomCode(); },1000);
Method 2:
var timeClock; function sendCode() { var timer_num = 60; timeClock=setInterval(function(){ timer_num--; $('.clock').html(timer_num); if (timer_num == 0) { clearInterval(timeClock); $('.clock').html(60); } },1000)
3. Get URL transmission parameters (support Chinese)
function getQueryString(name) { var reg = new RegExp('(^|&)' + name + '=([^&]*)(&|$)', 'i'); var r = (1).match(reg); if (r != null) { return decodeURI(r[2]); } return null; } //Calling methodGetQueryString("Parameter name")
4. Jq get form form data
function getFromData(id) { if (id == undefined) { id = "form" } var data = {}; var t = $(id).serializeArray(); $.each(t, function() { data[name = ] = ; }); return data; }
Calling method:
var userData. = getFromData(); userData.Formnamevalue //Get the value
5. Set, get, clear cookies
// Set cookies function setCookie(name, value) { var exp = new Date(); (() + 60 * 60 * 1000); = name + "=" + escape(value) + ";expires=" + () + ";path=/"; } //Read cookiesfunction getCookie(name) { var arr, reg = new RegExp("(^| )" + name + "=([^;]*)(;|$)"); if (arr = (reg)) return unescape(arr[2]); else return null; } // Clear all cookiesfunction clearCookie() { var keys = (/[^ =;]+(?=\=)/g); if (keys) { for (var i = ; i--;) { = keys[i] + '=0;path=/;expires=' + new Date(0).toUTCString(); //Clear the current domain name, for example: = keys[i] + '=0;path=/;domain=' + + ';expires=' + new Date(0).toUTCString(); //Clear the current domain name, for example. = keys[i] + '=0;path=/;domain=;expires=' + new Date(0).toUTCString(); //Clear the first-level domain name or specified, for example. } } }
6. js convert timestamp - convert to yyyy-MM-dd HH:mm:ss
//Timestamp conversion method date: timestamp numberfunction formatDate(date) { var date = new Date(date); var YY = () + '-'; var MM = (() + 1 < 10 ? '0' + (() + 1) : () + 1) + '-'; var DD = (() < 10 ? '0' + (()) : ()); var hh = (() < 10 ? '0' + () : ()) + ':'; var mm = (() < 10 ? '0' + () : ()) + ':'; var ss = (() < 10 ? '0' + () : ()); return YY + MM + DD +" "+hh + mm + ss; }
7. Canvas picture download (compatible with each browser)
// Save the picture in png format ("save").onclick = function () { var canvas = ("canvas"); if () {//ie browser var imgData = (); var blobObj = new Blob([imgData]); (blobObj, "Expert Certified QR Code.png"); } else {//Google Firefox Browser downLoad(("image/png")); } } // Download the picture function downLoad(url) { var oA = ("a"); = 'Expert Certified QR Code';// Set the downloaded file name, the default is 'Download' = url; = "qrcode" (oA); (); (); // After downloading, delete the created element }
8. Numbers and amount formats are converted to regular expressions
<input type="text" placeholder="Please enter" oninput = "checkInput(this)">
input input real-time judgment input is in amount format
function checkInput(obj) { var t = (0); = (/[^\d.]/g, ""); //Clear characters other than "number" and "." = (/^[0]+[0-9]*$/gi, ""); //The first digit cannot be 0 = (/^\./g, ""); //Verify that the first character is a number instead of. = (/\.{2,}/g, "."); //Retain only the first one. Clear the excess = (".", "$#$").replace(/\./g, "").replace("$#$", "."); = (/^(\-)*(\d+)\.(\d\d).*$/,'$1$2.$3'); //Only two decimals can be entered if (t == '-') { = '-' + ; } }
Number to amount format, retain two decimal places Example: Convert 1234567 to 1,234,567.00
//s is the number, n is the number of decimal points function fmoney(s, n) { n = n > 0 && n <= 20 ? n : 2; s = parseFloat((s + "").replace(/[^\d\.-]/g, "")).toFixed(n) + ""; var l = (".")[0].split("").reverse(), r = (".")[1]; t = ""; for (i = 0; i < ; i++) { t += l[i] + ((i + 1) % 3 == 0 && (i + 1) != ? "," : ""); } return ("").reverse().join("") + "." + r; }
Amount format to number Example: Convert 1,234,567.00 to 1234567.00
function moneyToNumValue(val) { var num = (); var ss = (); if ( == 0) { return "0"; } return (/,/g, ""); }
9. Canvas picture background is set to white or transparent
var canvas = ("canvas"); var imageData = ("2d").getImageData(0, 0, , ); for (var i = 0; i < ; i += 4) { // When the pixel is transparent, set to white if ([i + 3] == 0) { [i] = 255; [i + 1] = 255; [i + 2] = 255; [i + 3] = 255; } } ("2d").putImageData(imageData, 0, 0); var img = ("image/jpeg"); img = ((',') + 1); for (var i = 0; i < ; i += 4) { // When the pixel is white, set to transparent if ([i] == 255) { [i] = 0; [i + 1] = 0; [i + 2] = 0; [i + 3] = 0; } } ("2d").putImageData(imageData, 0, 0);
10. Commonly used regular expressions
//Mobile phone number regularvar reg = /^1[0-9]{10}$/; //Identity card number (18-digit) regularvar cP = /^[1-9]\d{5}(18|19|([23]\d))\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$/; // Email Regularvar reg = /^([a-zA-Z]|[0-9])(\w|\-)+@[a-zA-Z0-9]+\.([a-zA-Z]{2,4})$/; //Calling method!(phone) // means that the regular expression does not meet the mobile phone number
11. JavaScript gets the full current domain name
+"//"+; // Return; //Return the host part of the url, for example:; //return; //Return the entire url string (the complete address bar in the browser); //Return /a/or/; //Return the protocol part of the url, such as: http:, ftp:, maito:, etc. // If the default 80 port is used in the port part of url, the return value is not the default 80 but the empty character
12. Base64 picture compression
//Compress base64 methodfunction dealImage(base64, w, callback) { var newImage = new Image(); var quality = 0.6; //Compression coefficient between 0-1 = base64; ("crossOrigin", 'Anonymous'); // url needs to be used when it is out of the domain var imgWidth, imgHeight; = function() { imgWidth = ; imgHeight = ; var canvas = ("canvas"); var ctx = ("2d"); if ((imgWidth, imgHeight) > w) { if (imgWidth > imgHeight) { = w; = w * imgHeight / imgWidth; } else { = w; = w * imgWidth / imgHeight; } } else { = imgWidth; = imgHeight; quality = 0.6; } (0, 0, , ); (this, 0, 0, , ); var base64 = ("image/jpeg", quality); //Compressed statement callback(base64); //Return must be done through the callback function, otherwise the value will not be obtained in time } }
The above is the detailed content of the summary of commonly used js in work. For more information about the summary of commonly used js, please pay attention to my other related articles!