common常用方法和部分算法
var commonindex = function() {}; commonindex.prototype = {
ajaxRequest: function(request) {
$.ajax({
type: 'POST',
url: request.url,
data: request.data,
// dataType: 'json',
// contentType: "application/json",
success: function(data) {
//console.log(data);
request.handler_success(data);
},
error: function(xhr, type) {
request.handler_ex(xhr);
}
});
},
toast: function(msg) {
var str = "<div class='toast' style='display:none'>" + msg + "</div>";
$('body').append(str);
$('.toast').stop().fadeIn(100).delay(1000).fadeOut(400); //fade out after 3 seconds
setTimeout(function() {
$('.toast').remove();
}, 3000);
},
reset: function(ok, cannel) {
$("#toggleCSS").attr("href", "../../style/alertify.default.css");
alertify.set({
labels: {
ok: ok,
cancel: cannel
},
delay: 5000,
buttonReverse: false,
buttonFocus: "ok"
});
},
getChannelDomain: function(type) {
switch (type) {
case 'meizu':
return config.caiqr_domain;
break;
case 'caiqiu':
return config.caiqr_domain_mobile;
break;
}
},
getReferrer: function() {
var referrer = '';
try {
referrer = window.top.document.referrer;
} catch (e) {
if (window.parent) {
try {
referrer = window.parent.document.referrer;
} catch (e2) {
referrer = '';
}
}
}
if (referrer === '') {
referrer = document.referrer;
}
return referrer;
},
// 数组内排序
systemSort: function(arr) {
return arr.sort(function(a, b) {
return a - b;
});
},
// 从指定数组中随机n个数字
getArrayItems: function(arr, num) {
//新建一个数组,将传入的数组复制过来,用于运算,而不要直接操作传入的数组;
var temp_array = new Array();
arr.forEach(function(item) {
temp_array.push(item);
});
//取出的数值项,保存在此数组
var return_array = new Array();
for (var i = 0; i < num; i++) {
//判断如果数组还有可以取出的元素,以防下标越界
if (temp_array.length > 0) {
//在数组中产生一个随机索引
var arrIndex = Math.floor(Math.random() * temp_array.length);
//将此随机索引的对应的数组元素值复制出来
return_array[i] = temp_array[arrIndex];
//然后删掉此索引的数组元素,这时候temp_array变为新的数组
temp_array.splice(arrIndex, 1);
} else {
//数组中数据项取完后,退出循环,比如数组本来只有10项,但要求取出20项.
break;
}
}
return return_array;
},
// ajax get json
getJsonRequest: function(request) {
$.getJSON(request.url, function(responsedata, status, xhr) {
console.log('status======', status);
if (status == 'success') {
request.handler_success(responsedata);
} else {
request.handler_ex(xhr);
}
});
},
// 生成随机数函数
randomFn: function(max, min) {
return parseInt(Math.random() * (max - min + 1) + min, 10);
},
// 从max -min 中挑选n个不重复的数字
randomNrepatFn: function(n, min, max) {
var arr = [];
for (i = 0; i < n; i++) {
arr[i] = parseInt(Math.random() * (max - min + 1) + min);
for (j = 0; j < i; j++) {
if (arr[i] == arr[j]) {
i = i - 1;
break;
}
}
}
return arr;
},
//倒计时插件,doc元素节点,time:倒计时时间秒数,fun:回调函数,计时完成后回调该函数
getCountdown: function(doc, time, fun) {
if (doc) {
var minnew = Math.floor(time / 60);
var secnew = Math.floor(time % 60);
var hournew = 0;
if (time > 60) {
minnew = parseInt(time / 60);
if (minnew > 60) {
hournew = parseInt(min / 60);
minnew = parseInt(min % 60);
} else {
hournew = 0;
}
}
// 单位数加0
if (hournew < 10) {
hournew = '0' + hournew;
}
if (secnew < 10) {
secnew = '0' + secnew;
}
if (minnew < 10) {
minnew = '0' + minnew;
}
// 展示问题
if (hournew > 0 && minnew > 0) {
doc.innerText = hournew + ":" + minnew + ":" + secnew;
} else if (hournew == 0 && minnew > 0) {
doc.innerText = minnew + ":" + secnew;
} else if (hournew == 0 && minnew == 0) {
doc.innerText = '00:' + secnew;
}
}
var timer = setInterval(function() {
var min = Math.floor(time / 60);
var sec = Math.floor(time % 60);
var hour = 0;
if (time > 60) {
min = parseInt(time / 60);
if (min > 60) {
hour = parseInt(min / 60);
min = parseInt(min % 60);
} else {
hour = 0;
}
}
time--;
// 单位数加0
if (hour < 10) {
hour = '0' + hour;
}
if (sec < 10) {
sec = '0' + sec;
}
if (min < 10) {
min = '0' + min;
}
// 展示问题
if (hour > 0 && min > 0) {
doc.innerText = hour + ":" + min + ":" + sec;
} else if (hour == 0 && min > 0) {
doc.innerText = min + ":" + sec;
} else if (hour == 0 && min == 0) {
doc.innerText = '00:' + sec;
}
// 执行回调函数
if (time <= 0) {
fun();
clearInterval(timer);
return;
}
}, 1000);
},
// 大数组内进行排列组合
comarray: function(arr) {
var sarr = [
[]
];
for (var i = 0; i < arr.length; i++) {
var tarr = [];
for (var j = 0; j < sarr.length; j++)
for (var k = 0; k < arr[i].length; k++) {
tarr.push(sarr[j].concat(arr[i][k]));
}
sarr = tarr;
}
return sarr;
},
//大写转化为小写
toLowerCase: function(comeStr) {
return comeStr.toLowerCase()
}
}; //数组的组合
Array.prototype.combinate = function(iItems, aIn) {
if (!aIn) {
var aIn = new Array();
this.combinate.aResult = new Array();
}
for (var i = 0; i < this.length; i++) {
var a = aIn.concat(this[i]);
var aRest = this.concat();
aRest.splice(0, i + 1);
if (iItems && iItems - 1 <= aRest.length) {
aRest.combinate(iItems - 1, a);
if (iItems == 1) this.combinate.aResult.push(a);
}
}
return this.combinate.aResult;
}; Array.prototype.add = function(number) {
return this.map(function(item) {
return item + number;
});
}; //根据域名修改渠道id
var commonObject = new commonindex();
commonindex.prototype.ajaxRequest = function(request) {
if (window.location.href.indexOf(commonObject.getChannelDomain('meizu')) > -1) {
request.data.channel = "meizuh5";
request.data.client_type = '4';
}
if (window.location.href.indexOf(commonObject.getChannelDomain('caiqiu')) > -1) {
request.data.channel = "lingshengcaijiandashi01";
}
$.ajax({
type: 'POST',
url: request.url,
data: request.data,
// dataType: 'json',
// contentType: "application/json",
success: function(data) {
//console.log(data);
request.handler_success(data);
},
error: function(xhr, type) {
request.handler_ex(xhr);
}
});
};
common常用方法和部分算法的更多相关文章
- POJ 1330 Nearest Common Ancestors (LCA,倍增算法,在线算法)
/* *********************************************** Author :kuangbin Created Time :2013-9-5 9:45:17 F ...
- B - Common Divisors (codeforces)数论算法基本定理,唯一分解定理模板
You are given an array aa consisting of nn integers. Your task is to say the number of such positive ...
- poj1330Nearest Common Ancestors 1470 Closest Common Ancestors(LCA算法)
LCA思想:http://www.cnblogs.com/hujunzheng/p/3945885.html 在求解最近公共祖先为问题上,用到的是Tarjan的思想,从根结点开始形成一棵深搜树,非常好 ...
- 动态规划 算法(DP)
多阶段决策过程(multistep decision process)是指这样一类特殊的活动过程,过程可以按时间顺序分解成若干个相互联系的阶段,在每一个阶段都需要做出决策,全部过程的决策是一个决策序列 ...
- 【图像配准】基于灰度的模板匹配算法(一):MAD、SAD、SSD、MSD、NCC、SSDA、SATD算法
简介: 本文主要介绍几种基于灰度的图像匹配算法:平均绝对差算法(MAD).绝对误差和算法(SAD).误差平方和算法(SSD).平均误差平方和算法(MSD).归一化积相关算法(NCC).序贯相似性检测算 ...
- 【算法导论】最小生成树之Kruskal法
在图论中,树是指无回路存在的连通图.一个连通图的生成树是指包含了所有顶点的树.如果把生成树的边的权值总和作为生成树的权,那么权值最小的生成树就称为最小生成树.因为最小生成树在实际中有很多应用,所以我们 ...
- 排序算法Java代码实现(一)—— 选择排序
以下几篇随笔都是记录的我实现八大排序的代码,主要是贴出代码吧,讲解什么的都没有,主要是为了方便我自己复习,哈哈,如果看不明白,也不要说我坑哦! 本片分为两部分代码: 常用方法封装 排序算法里需要频繁使 ...
- 剖析ironic
关键技术 在安装操作系统时需要存储介质来存储系统镜像.需要控制物理机开关机,在网络部署环境中还需要预启动环境. PXE (预启动环境) IPMI(电源管理) iSCSI(存储) 什么是PXE PXE( ...
- coursera机器学习-聚类,降维,主成分分析
#对coursera上Andrew Ng老师开的机器学习课程的笔记和心得: #注:此笔记是我自己认为本节课里比较重要.难理解或容易忘记的内容并做了些补充,并非是课堂详细笔记和要点: #标记为<补 ...
随机推荐
- winPcap_5_打开适配器并捕获数据包
知道如何获取适配器的信息了,那我们就开始一项更具意义的工作,打开适配器并捕获数据包.编写一个程序,将每一个通过适配器的数据包打印出来. 打开设备的函数是 pcap_open(). (Open a ge ...
- Flume源码-LoggerSink
package org.apache.flume.sink; import com.google.common.base.Strings; import org.apache.flume.Channe ...
- BitmapFactory.decodeByteArray() 返回null,分析与解决
问题描述:用android自带的Camera获取图片,上传至远程数据库中(mysql),以BLOB格式存储, 但在提取图片时,始终无法在android界面显示,示例代码如下: ..... .... ...
- HUNNU--湖师大--11410--Eligibility
[I] Eligibility Regional Contest Director Osama Ismail knows that an individual cannot participate i ...
- Robotium--通过Id寻找控件
在自动化测试中,UI上经常有一些控件是没有名称的,那么此时,就可以通过id来找到这些控件. 案例:对两个EditText进行测试 package com.tangbc.tedit.test; impo ...
- [Flexbox] Using flex-direction to layout content horizontally and vertically
The Flexbox css spec allows for more adjustable layouts. The flex-directionproperty allows you to ea ...
- [Redux] Writing a Todo List Reducer (Toggling a Todo)
Learn how to implement toggling a todo in a todo list application reducer. let todo = (state = [], a ...
- WIN32读写INI文件方法
在程序中经常要用到设置或者其他少量数据的存盘,以便程序在下一次执行的时候可以使用,比如说保存本次程序执行时窗口的位置.大小.一些用户设置的 数据等等,在 Dos 下编程的时候,我们一般自己产生一个 ...
- 通知中心 NSNotificationCenter
NSNotificationCenter 通知中心提供了一种在程序内广播信息的途径,一个NSNotificationCenter对象本质上是一个通知分发表(notification dispatch ...
- HDU -2670 Girl Love Value
这道题是刚好装满的背包问题,刚好选取k个,状态转移方程为dp[i][j] = max( dp[i - 1][j], dp[i - 1][j - 1] + Li - Bi(j - 1) ) dp[i][ ...