本文轉載自:https://blog.csdn.net/xiaogeldx/article/details/85455011javascript
random:隨機數java
document.write(Math.sqrt(16)+'<br>'); //開方 4 document.write(Math.abs(-1)+'<br>'); //絕對值 1 document.write(Math.PI*2+'<br>'); //π 6.28..... document.write(Math.pow(2,3)+'<br>'); //x的y次方 8 document.write(Math.round(3.6)+'<br>'); //取整,四捨五入 4 document.write(Math.floor(3.2)+'<br>'); //向下取整 3 document.write(Math.ceil(3.2)+'<br>'); //向上取整 4 document.write(Math.max(1,34,23)+'<br>'); //最大值 34 document.write(Math.min(1,34,23)+'<br>'); //最小值 1 document.write(Math.random()+'<br>');//0-1以內的隨機數 document.write(Math.random()*100+'<br>'); //0-100隨機數
時間戳:格林威治時間1970年01月01日00時00分00秒(北京時間1970年01月01日08時00分00秒)起至如今的總秒數dom
var today = new Date(); document.write(today+'<br>'); //Mon Dec 31 2018 10:22:51 GMT+0800 (中國標準時間) var year = today.getFullYear(); document.write(year+'<br>'); //2018 var month = today.getMonth()+1; // 從 Date 對象返回月份 (0 ~ 11) document.write(month+'<br>'); //12 var day = today.getDay(); //從 Date 對象返回一週中的某一天 (0 ~ 6) document.write(day+'<br>'); //1週一 var day1 = today.getDate(); // document.write(day1+'<br>'); //31三十一號 var hour = today.getHours(); document.write(hour+'<br>'); //10(時) var minute = today.getMinutes(); document.write(minute+'<br>'); //34(分) var second = today.getSeconds(); document.write(second+'<br>'); //10(秒) document.write(year+'/'+month+'/'+day1+'/'+hour+':'+minute+':'+second) // 2018/12/31/10:37:14 document.write(Date.now()) //1546224045934(ms)
匿名函數函數
function add(參數) {
alert("我被調用了!");
} //後面不用;結尾
add() //調用.net
var box = document.getElementsByTagName("div")[0];
box.onclick = function () {
alert("434");
}; //後面用;結尾code
function ase(a) { alert(a); } ase(222);
function a() { console.log(arguments);//固定用arguments } a(1,3,7,'xiaoge');
function b(a,b) {
console.log(arguments);
console.log(a,b)
}
b(1,3,7,'xiaoge');
對象
自執行blog
!function () { document.write("w"+"<br>") }(); //w +function () { document.write("we"+"<br>") }(); //we -function () { document.write("wer"+"<br>") }(); //wer (function () { document.write("wert"+"<br>") })(); //wert (function () { document.write("wertu"+"<br>") }()); //wertu
//var 是定義局部變量 var a = 100; function func() { var a = 200; alert(a); } alert(a); func(); alert(a) //100,200,100 //不用var是定義全局變量 var a = 100; function func() { a = 200; alert(a); } alert(a); func(); alert(a) //100,200,200
清除定時器的時候要給定時器加個名字圖片
<body> <button>stop</button> <script type="text/javascript"> setTimeout(function () { console.log(123); },2000); //兩秒後執行一次,再也不執行 2000ms,1s=1000ms var a = setInterval(function () { console.log(321); },2000); //每兩秒執行一次 var b = document.getElementsByTagName('button')[0]; b.onclick = function () { clearInterval(a); }; </script> </body>