// 記錄當前節流標誌
let canSend = true;
//獲取input元素
function getValue(){
const value = document.getElementById("textValue").value;
throttle(sendAjax, value);
}
//點擊屢次的狀況下節流處理
function throttle(fn, value){
if (canSend){
canSend = false;
fn(value).then(res => {alert(res)});
setTimeout(function(){
canSend = true;
}, 2000)
}
}
function sendAjax(value){
var changeValueData = changeValue(value);
var options = {url: 'url',method: 'GET',async: true,data: changeValueData,dataType: 'text'};
return new Promise(function (resolve, reject){
var xhr = new XMLHttpRequest();
xhr.open(options.method, options.url, options.async);
xhr.onReadyStateChange = function(){
if (xhr.readyState === 4 && xhr.status === 200){
resolve(xhr.responseText, xhr)
}
}
xhr.onerror = (err) => {
reject(err)
}
xhr.send();
})
}
function changeValue(value){
const valueArr = value.split("");
let obj = {};
let timeArr = [];
let minTimes;
let res = '';
valueArr.map(key => {
if (!obj[key]){
obj[key] = 1
}else{
obj[key]++;
}
});
for (let key in obj){
timeArr.push(obj[key])
};
timeArr = Array.from(new Set(timeArr));
if (timeArr.length === 1){
res = value;
}else{
minTimes = timeArr.sort()[0];
}
for (let i = 0; i < value.length; i++){
if (obj[value.charAt(i)] > minTimes){
res += value.charAt(i);
}
};
return res;
}
/** * @param number @return string */
const toChineseNum = (num) => {
let dtext=['','十','百','千','萬']
let len=num.toString().length
let numArr=num.toString().split('')
let numTxt=''
const toT=(numIndex)=>{
const arr=['零','一','二','三','四','五','六','七','八','九']
return arr[numIndex]
}
for(let i=1;i<=len;i++){
if(len>5&&i<(len-3)){
if(i==(len-4)){
numTxt+=numArr[i-1]==0?'':toT(numArr[i-1])
numTxt+='萬'
}else{
if((numArr[i-2]==0&&numArr[i-1]==0)||((numArr[i-1]==0)&&(numArr[len-5]==0)&&numArr[i]==0)){
numTxt+=''
}else{
numTxt+=(((numArr[i-1]==0)&&(numArr[len-5]!=0))||(numArr[i]!=0&&numArr[i-1]==0))?'零':(toT(numArr[i-1])+dtext[len-i-4])
}
}}else{
if((numArr[i-1]==0&&i==len)||(numArr[i]==0&&numArr[i-1]==0)){
numTxt+=''
}else{
numTxt+=numArr[i-1]==0?'零':(toT(numArr[i-1])+dtext[len-i])
}
}}
return numTxt
}
console.log(toChineseNum(1088701));
複製代碼
// // // javascript