这一章节我们来介绍Javascript的Math对象,日期对象,函数以及定时器:
Javascript的Math对象
| 参数 |
描述 |
| sqrt |
开平方 |
| abs |
绝对值 |
| PI |
圆周率π |
| pow |
x的y次方 |
| round |
取整-四舍五入 |
| floor |
取整-向下 |
| ceil |
取整-向上 |
| max |
最大值 |
| min |
最小值 |
| random |
随机数 |
<script type="text/javascript">
// Math模块
document.write("开平方:"+Math.sqrt(81)+"<br>")//开平方
document.write("绝对值:"+Math.abs(-4)+"<br>")//绝对值
document.write("圆周率π:"+Math.PI+"<br>")//圆周率π
document.write("x的y次方:"+Math.pow(2,10)+"<br>")//x的y次方
document.write("取整-四舍五入:"+Math.round(4.5)+"<br>")//取整-四舍五入
document.write("取整-向下:"+Math.floor(4.9)+"<br>")//取整-向下
document.write("取整-向上:"+Math.ceil(4.1)+"<br>")//取整-向上
document.write("最大值:"+Math.max(1,3,8,5,98,76,1567)+"<br>")//最大值
document.write("最小值:"+Math.min(1,2,4,6,7,-3,5)+"<br>")//最小值
document.write("随机数(1-0)函数默认:"+Math.random()+"<br>")//随机数1-0
document.write("随机数(1-100)放大100倍:"+Math.round(Math.random()*100)+"<br>")//随机数1-100
</script>
日期对象
| 参数 |
描述 |
| getFullYear() |
获取年 |
| getMonth() |
获取月 |
| getDate() |
获取天 |
| getHours() |
获取小时 |
| getMinutes() |
或取分 |
| getSeconds() |
获取秒 |
| today.getDay() |
获取星期 |
| Date.now() |
时间戳 |
// 日期对象
var today = new Date();
document.write('当前时间:'+today+"<br>");//获取当前时间
var year = today.getFullYear();//获取年
document.write('年:'+year+"<br>");
var month = today.getMonth()+1;//获取月
document.write('月:'+month+"<br>");
var date = today.getDate();//获取日
document.write('日:'+date+"<br>")
var hour = today.getHours();//获取时
document.write('时:'+hour+"<br>")
var min = today.getMinutes();//获取分
document.write('分:'+min+"<br>")
var sec = today.getSeconds();//获取秒
document.write('秒:'+sec+"<br>")
document.write('星期:'+today.getDay()+"<br>");//获取星期
document.write("现在时间为:"+year+'/'+month+'/'+date+'/'+' '+hour+':'+min+':'+sec+"<br>");
// 时间戳-获取的是毫秒,Python获取的是秒
var timestamp = Date.now();
document.write("当前的时间戳是:"+timestamp+"<br>")
函数
函数是通常是把一系列重复使用的操作封装成一个方法,方便调用
函数的定义:function funName(){}
函数分为:有名函数,匿名函数
// 函数-有名函数
function func1() {
document.write('有名函数的值:'+123+"<br>");
}
func1();
// 函数-传参
function func2(a,b) {
document.write('函数传参:'+a*b+"<br>");
}
func2(2,3);
// 函数-返回值
function func3(a) {
return a*10;
}
document.write('函数func的值'+func3(5)+"<br>");
// 不定参
function func4(a,b) {
document.write('不定长参数:'+arguments+"<br>");
document.write(a,b)
}
func4(1,2,3,4,5);
var a = 100;
function func5() {
a = 200;
document.write('内部变量没有var:'+a+"<br>")
}
document.write('内部变量没有var:'+a+"<br>");
func5();
document.write('内部变量没有var:'+a+"<br>");
var a = 100;
function func6() {
var a = 200;
document.write('内部变量有var:'+a+"<br>");
}
document.write('内部变量有var:'+a+"<br>");
func6();
document.write('内部变量有var:'+a+"<br>")
//自调用函数
!function () {
alert(123)
}();
+function () {
alert(123)
}();
-function () {
alert(123)
}();
~function () {
alert(123)
}();
定时器及应用
| 参数 |
描述 |
| setTimeout |
设置定时器,只会执行一次 |
| clearTimeout |
清除定时器,针对上面的定时器,不会经常用 |
| setInterval |
设置定时器,会一直更新执行 |
| clearInterval |
清除定时器,针对setInterval定时器 |
// 定时器
// 只调用一次
setTimeout(function () {
document.write("这是settimeout的调用"+"<br>")
},2000);
//每隔2秒执行一次
var timer = setInterval(function () {
document.write("这是setInterval的调用"+"<br>")
},2000);
//清除定时器
clearInterval(timer)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>JS-3-作业</title>
<style>
.div1{
width: 800px;
height: 300px;
background-color: wheat;
margin: auto;
}
.div2{
width: 100%;
height: 150px;
border:1px solid red;
}
#p1{
text-align: center;
margin: 60px 0;
font-size: 30px;
color: #606060;
}
#p2{
text-align: center;
margin: 50px 0;
font-size: 35px;
color: #606060;
}
</style>
</head>
<body>
<div class="div1">
<div class="div2">
<p id="p1"></p>
</div>
<div class="div2">
<p id="p2">北京时间:
<span class="spa">
</span>
<span class="spa">
</span>
</p>
</div>
</div>
<script>
var box = document.getElementById("p1");
count=30;
function counttime() {
count--;
if(count<10){count="0"+count}
if(count>0) {
box.innerText = "敌军还有 " + count + " 秒到达战场"
}else{
box.innerText = "全军出击!!";
clearInterval(timein)
}
};
var timein = setInterval("counttime()",1000);
var spa = document.getElementsByClassName("spa")
function timmer() {
var today = new Date();
var year = today.getFullYear();
var month =today.getMonth();
var day =today.getDate();
var hour =today.getHours();
var min =today.getMinutes();
var sec =today.getSeconds();
if(hour<10){hour="0"+hour};
if(min<10){min="0"+min};
if(sec<10){sec="0"+sec};
spa[0].innerText = year+"年"+month+"月"+day+"日"+" ";
spa[1].innerText = hour+":"+min+":"+sec
}
setInterval("timmer()",1000)
</script>
</body>
</html>
网友评论