JavaScript 案例17
数组
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>shuzu</title>
</head>
<body>
<script>
var arr = [1,2,3];
alert(arr.length);
arr.push(2,2)
alert(arr);
arr.pop()
alert(arr);
arr.splice(2,2);
alert(arr);
arr.splice(2,0,'1','a','c');
alert(arr);
arr.splice(1,2,'d','e')
alert(arr)
var a = [1,2,3]
var b = ['a','v','c','d'];
alert(a.concat(b));
alert(a.join("--"))
var s = ['abc','fds','qqe','zdsa','dsf'];
s.sort();
alert(s);
var ss = [12,1,23,43,2,31,78,5,7];
alert(ss.sort());
ss.sort(function (n1, n2) {
if(n1<n2){
return -1;
}else if(n1>n2){
return 1;
}else {
return 0;
}
})
alert(ss);
ss.sort(function (n1, n2) {
return n1-n2;
})
alert(ss);
</script>
</body>
</html>
JavaScript 案例18
定时器
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<input type="button" value="开启定时器" id="start">
<input type="button" value="关闭定时器" id="close">
<script>
function show() {
alert("aa");
}
setInterval(show,2000);
setTimeout(show,2000);
var start = document.getElementById("start");
var close = document.getElementById("close");
var timer = null;
start.onclick = function () {
timer = setInterval(function () {
alert("22")
},1000);
}
close.onclick = function () {
clearInterval(timer);
}
</script>
</body>
</html>