Summary of common js functions in work
- 2021-09-16 05:59:12
- OfStack
1. Prevent repeated clicks in javascript and prevent clicks from being too fast
To prevent repeated clicks, a switch can be added, so that this switch defaults to true, and the first click changes it to false. To execute the click event, it is necessary to judge whether this switch is true, executed for true, and not executed for false. 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 to automatically change the switch to true after 1 fixed time. The following example is that after 500 milliseconds, the switch automatically changes 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 realizes 60-second countdown
Method 1:
var time = 60;
// Countdown
function 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 (Chinese is supported)
function getQueryString(name) {
var reg = new RegExp('(^|&)' + name + '=([^&]*)(&|$)', 'i');
var r = window.location.search.substr(1).match(reg);
if (r != null) {
return decodeURI(r[2]);
}
return null;
}
// Invoke method
GetQueryString(" Parameter name ")
4. Jq Getting from Form Data
function getFromData(id) {
if (id == undefined) {
id = "form"
}
var data = {};
var t = $(id).serializeArray();
$.each(t, function() {
data[name = this.name] = this.value;
});
return data;
}
Invoke the method:
var userData. = getFromData();
userData. Form name Value // Get a value
5. Set, get and empty Cookie
// Settings cookies
function setCookie(name, value) {
var exp = new Date();
exp.setTime(exp.getTime() + 60 * 60 * 1000);
document.cookie = name + "=" + escape(value) + ";expires=" + exp.toGMTString() + ";path=/";
}
// Read cookies
function getCookie(name) {
var arr, reg = new RegExp("(^| )" + name + "=([^;]*)(;|$)");
if (arr = document.cookie.match(reg))
return unescape(arr[2]);
else
return null;
}
// Know everything cookies
function clearCookie() {
var keys = document.cookie.match(/[^ =;]+(?=\=)/g);
if (keys) {
for (var i = keys.length; i--;) {
document.cookie = keys[i] + '=0;path=/;expires=' + new Date(0).toUTCString(); // Clear the , For example: m.kevis.com
document.cookie = keys[i] + '=0;path=/;domain=' + document.domain + ';expires=' + new Date(0).toUTCString(); // Under the current domain name, for example .m.kevis.com
document.cookie = keys[i] + '=0;path=/;domain=kevis.com;expires=' + new Date(0).toUTCString(); // Clear 1 Class domain name or specified, such as .kevis.com
}
}
}
6. js Conversion Timestamp-Conversion to yyyy-MM-dd HH: mm: ss
// Time stamp conversion method date: Time stamp number
function formatDate(date) {
var date = new Date(date);
var YY = date.getFullYear() + '-';
var MM = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-';
var DD = (date.getDate() < 10 ? '0' + (date.getDate()) : date.getDate());
var hh = (date.getHours() < 10 ? '0' + date.getHours() : date.getHours()) + ':';
var mm = (date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes()) + ':';
var ss = (date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds());
return YY + MM + DD +" "+hh + mm + ss;
}
7. canvas Image Download (compatible with all browsers)
// Save as png Format pictures
document.getElementById("save").onclick = function () {
var canvas = document.getElementById("canvas");
if (window.navigator.msSaveOrOpenBlob) {//ie Browser
var imgData = canvas.msToBlob();
var blobObj = new Blob([imgData]);
window.navigator.msSaveOrOpenBlob(blobObj, " Expert certification 2 Dimension code .png");
} else {// Google Firefox Browser
downLoad(canvas.toDataURL("image/png"));
}
}
// Download pictures
function downLoad(url) {
var oA = document.createElement("a");
oA.download = ' Expert certification 2 Dimension code ';// Set the file name for download, and the default is ' Download '
oA.href = url;
oA.className = "qrcode"
document.body.appendChild(oA);
oA.click();
oA.remove(); // Delete the created elements after downloading
}
8. Number and Amount Formats Transform Regular Expressions
var isclick= true;
function click(){
if(isclick){
isclick= false;
// Add the events to be executed below
...
// Timer
setTimeout(function(){
isclick = true;
}, 500);
}
0
input input real-time judgment input is in amount format
var isclick= true;
function click(){
if(isclick){
isclick= false;
// Add the events to be executed below
...
// Timer
setTimeout(function(){
isclick = true;
}, 500);
}
1
Number to Amount Format, Keep Two Decimal Places Example: Convert 1234567 to 1,234,567.00
var isclick= true;
function click(){
if(isclick){
isclick= false;
// Add the events to be executed below
...
// Timer
setTimeout(function(){
isclick = true;
}, 500);
}
2
Amount Format to Number Example: Convert 1,234,567.00 to 1234567.00
function moneyToNumValue(val) {
var num = val.trim();
var ss = num.toString();
if (ss.length == 0) {
return "0";
}
return ss.replace(/,/g, "");
}
9. canvas image background set to white or transparent
var isclick= true;
function click(){
if(isclick){
isclick= false;
// Add the events to be executed below
...
// Timer
setTimeout(function(){
isclick = true;
}, 500);
}
4
10. Common regular expressions
var isclick= true;
function click(){
if(isclick){
isclick= false;
// Add the events to be executed below
...
// Timer
setTimeout(function(){
isclick = true;
}, 500);
}
5
101. JavaScript Get the full current domain name
var isclick= true;
function click(){
if(isclick){
isclick= false;
// Add the events to be executed below
...
// Timer
setTimeout(function(){
isclick = true;
}, 500);
}
6
102. base64 Image Compression
var isclick= true;
function click(){
if(isclick){
isclick= false;
// Add the events to be executed below
...
// Timer
setTimeout(function(){
isclick = true;
}, 500);
}
7
The above is the detailed content of the summary of js commonly used in the work. Please pay attention to other related articles on this site for more information about the summary of js commonly used!