28-js函数

129 阅读1分钟

不定参数

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>fun</title>
</head>
<body>
    <script>
        function foo(name, age) {
            console.log('hello'+name+age);
        }
        console.log(foo.length);
        //多个值相加,不定参数的使用
        function add() {
            var sum = 0;
            for (var i = 0; i<arguments.length;i++){
                sum+=arguments[i];
            }
            return sum;
        }
        add(1,2,3,4,5);
        //匿名函数
        var func1 = function (arg) {
            return arg;
        }
        func1("xiaoming");



    </script>
</body>
</html>

setInterval

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>BOM对象</title>
    <style>
        #id1{
            width: 200px;
            height: 30px;
        }
    </style>
</head>
<body>
    <script>
        /*window.alert("fengfeng")
        var a = window.confirm("hello")
        console.log(a)
        window.prompt("zhangyafeng")
        open("http://www.baidu.com")*/
        var click1;
        function begin() {
            clearInterval(click1);
            showtime();
            click1 = setInterval(showtime,1000);
        }
        function showtime() {
            var time = new Date().toLocaleString();
            var ele = document.getElementById("id1");
            ele.value=time;
        }
        function stopAction() {
            clearInterval(click1);
            //click1=undefined;
        }
        function feng() {
            alert("xiaoming")

        }
        //三秒钟后执行
        var c = setTimeout(feng,3000);
        clearTimeout(c);
    </script>
    <input type="text" id="id1" onclick="begin()">
    <button onclick="stopAction()">停止</button>
</body>
</html>

History和Location

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>history</title>
</head>
<body>
    <script>
        function intoaction() {
            history.forward();

        }
        //刷新
        location.reload();
        location.assign("http://www.baidu.com");
        location.replace("http://www.baidu.com");
    </script>
    <a href="history1.html">click</a>
    <button onclick="intoaction()">into</button>


</body>
</html>