复习
-
var timer1 = setTimeout(fn,1000);
-
var timer2 = setInterval(fn,1000);
-
clearTimeout(timer2)
-
clearInterval(timer1)
setTimeout(function(){ console.log(111); },0) console.log(222); for(var i = 0; i < 10; i++){ setTimeout(function(){ console.log(i); },0); }
function move(obj,attr,step,target){ clearInterval(obj.timer); var current = parseInt(getCss(obj,attr)); step = target >= current ? step : -step; obj.timer = setInterval(function(){ current += step; if(step<0&¤t<=target||step>0&¤t>=target){ clearInterval(obj.timer); current = target; } obj.style[attr] = current + 'px'; },20); } move(oBox,'left',10,500);
(一)时间对象
-
1- instanceof 判断对象是否属于某个类的实例
String Number Boolean Array Object RegExp Function
- 基本数据类型通过字面量方式创建的值不能完全称之为对象,所以不能被instanceof检测出来属于对应类的实例
var str = 'hello'; //字面量方式创建 console.log(str instanceof String); //false
- 如果通过new 的方式创建,就可以称之为对象,就可以正常检测
var str = new String('world'); console.log(str instanceof String); //true console.log(str instanceof Object); //true
-
2- Date对象
- 创建时间对象
new Date();
//获取系统当前时间 var date1 = new Date(); console.log(date1); // //指定时间创建时间对象 var date2 = new Date(2018,11,11); console.log(date2); var date3 = new Date('2018-12-12 12:00:00'); console.log(date3); var date4 = new Date('2018/12/12 12:00:00'); console.log(date4); var date5 = new Date('Tue Jul 02 2019'); console.log(date5);
- 获取时间对象的每个部分的值
//获取系统当前时间 var date1 = new Date(); console.log(date1); //获取年份 console.log(date1.getFullYear()); //获取月份 console.log(date1.getMonth()); // 0-11 //获取日期 console.log(date1.getDate()); // 1-31 //获取星期 console.log(date1.getDay()); // 0-6 //获取小时 console.log(date1.getHours()); // 0-23 //获取分钟 console.log(date1.getMinutes()); // 0-59 //获取秒数 console.log(date1.getSeconds()); // 0-59 //获取毫秒数 console.log(date1.getMilliseconds()); // 0-999 //获取当前时间对象距离1970年1月1日 00:00:00的毫秒数 console.log(date1.getTime());
- 修改时间对象
//获取系统当前时间 var date1 = new Date(); console.log(date1); //设置年份 date1.setFullYear(2018); console.log(date1); //设置月份 date1.setMonth(7); console.log(date1); //设置日期 date1.setDate(3); console.log(date1); //设置小时,一个小时之后的时间 date1.setHours(date1.getHours()+1); console.log(date1); //设置分钟 date1.setMinutes(15); console.log(date1); //设置秒 date1.setSeconds(15); console.log(date1); //设置毫秒 date1.setMilliseconds(15); console.log(date1.getMilliseconds());
- 格式化时间方法
var date = new Date(); var d1 = date.toDateString(); console.log(d1);// 'Tue Jul 02 2019' var d2 = date.toTimeString(); console.log(d2); // '14:07:42 GMT+0800 (中国标准时间)' var d3 = date.toLocaleDateString(); console.log(d3); // '2019/7/2' var d4 = date.toLocaleTimeString(); console.log(d4); // '下午2:07:42'
(二) 字符串方法
-
1- charAt(index) 根据索引获取字符
-
2- charCodeAt(index) 根据索引获取字符的编码
-
3- String.fromCharCode(code) 把编码转换成字符
-
4- indexOf(char) 查找字符在字符串中第一次出现的索引,如果不存在返回-1
-
5- lastIndexOf(char) 查找字符在字符串中最后一次出现的索引,如果不存在返回-1
-
6- toUpperCase() 转换成大写
-
7- toLowerCase() 转换成小写
-
8- substring(star,end) 复制字符串中的指定字符
var str = "hello weorld!"; var res = str.substring(2); // 从索引2开始复制到末尾 console.log(res); var res = str.substring(2,4); // 从索引2开始复制到索引4,不包含4 console.log(res); var res = str.substring(4,2); // 自动交换位置 (2,4) console.log(res); var res = str.substring(-3,4); // 负数当0处理 (0,4) console.log(res);
-
9- slice() 复制字符串中的指定字符
var res = str.slice(2); // 从索引2开始复制到末尾 console.log(res); var res = str.slice(2,4); // 从索引2开始复制到索引4,不包含4 console.log(res); var res = str.slice(4,2); //前面数大,结果为 '' console.log(res); var res = str.slice(-10,4); // -10+长度 (3,4) console.log(res); var res = str.slice(-6,4); // -10+长度 (7,4) 前面数大,结果为 '' console.log(res);
-
10- split() 已制定的分割符把字符串分割成数组
var str = "hel,lo weo-rld www qqq"; var res = str.split(); //没有参数,不分割,整体放入数组 console.log(res); //["hel,lo weo-rld www qqq"] var res = str.split('');//参数为空字符串,每个字符都单独分割 console.log(res); //["h", "e", "l", ",", "l", "o", " ", "w", "e", "o", "-", "r", "l", "d", " ", "w", "w", "w", " ", "q", "q", "q"] var res = str.split(' ');//参数为空格,以空格字符分开 console.log(res); //["hel,lo", "weo-rld", "www", "qqq"] var res = str.split(/[, -]/);//以多个字符(逗号,空格,连字符的位置都断开)分割字符串 console.log(res); // ["hel", "lo", "weo", "rld", "www", "qqq"]
-
11- replace(old,new); 用新的字符替换旧的字符
var str = "hello weorld"; var res = str.replace('e','E'); console.log(res); // 'hEllo weorld';只替换一个 var str = "heelo weorld"; var res = str.replace(/e/g,'E'); // 有几个e就替换几个 console.log(res); //'hEElo wEorld'
来源:https://www.cnblogs.com/didamehulayou/p/11122067.html