zoukankan      html  css  js  c++  java
  • JavaScript-数学对象与定时器

    数学对象

    数学对象的操作

    console.log(Math);
     

    1.取整的方法

        //针对于负数来说:
        
        //向下取整
        console.log( Math.floor(4.5) ); //4
    
        //向上取整
        console.log(Math.ceil(4.001)); //5
    
        //四舍五入
        console.log(Math.round(4.4)); //4
        console.log(Math.round(4.5)); //5
    
        //舍去小数部分
        console.log(Math.trunc(-8.5))//-8
    
    
    
        /* //针对于负数来说:(五舍四入)
        console.log(Math.floor(-8.7)); //-9
        console.log(Math.ceil(-8.7)); //-8
    
        console.log(Math.trunc(-8.5))//-8
        console.log(Math.trunc(-8.9))//-8
        console.log(Math.trunc(-8.1))//-8*/
    
    
        // console.log( Math.round(-6.3) );//-6
        // console.log( Math.round(-6.4) );//-6
        // console.log( Math.round(-6.5) );//-6
        // console.log( Math.round(-6.6) );//-7

    2.随机数

        /*//生成一个 [ 0 ~ 1 ) 的随机数
        console.log(Math.random());
        console.log(Math.random());
    
        //生成一个 [0~100)的随机数
        console.log( Math.random()*100 );
        console.log( Math.random()*100 );
    
        //生成一个 0~10 的随机整数
        console.log(Math.floor( Math.random() * 11) );
        console.log(Math.floor( Math.random() * 11) );*/
    
    
        //生成一个 a~b 的随机整数
        // Math.random() * (b-a+1) + a
        let a = 10,b =30;
        for (let i=0;i<100;i++){
          console.log(Math.floor(Math.random() * (b-a+1) + a));
        }

    3.最大值与最小值

        /*//取出最大的
        let x = Math.max( 1,5,4,88,77,99.2,66,22,5,3 );
        console.log(x);
    
        //取出最小的
        let y = Math.min( 1,5,4,88,77,99.2,66,22,5,3 );
        console.log(y);*/
    
        //得到数组中的最大数字
        let arr = [88,4,3,56,91,52,46];
    
        let m = Math.max.apply(Math,arr);
        console.log(m);
    
        Math.max.apply(Math,arr);

    定时器

    含义:隔一段时间之后,去执行代码

    单次定时器:setTimeout()  循环定时器:setInterval()
    /*setTimeout( function(){
          console.log(1);
        } , 3000 );
    
        console.log(2);*/
    
        setInterval( function(){
          console.log(1);
        } , 3000 );
    
        console.log(2);

    停止定时器

     清除单次定时器:clearTimeout()  清除循环定时器:clearInterval()

        //如果需要清除定时器,那么我需要知道对应定时器的id
        
        /*let id = setTimeout(function(){
          console.log(123);
        },5000);
    
        let btn = document.getElementById("btn");
        btn.onclick = function(){
          clearTimeout(id);
        };*/
    
        let timer = setInterval(function(){
          console.log(123);
        },1000);
    
        let btn = document.getElementById("btn");
        btn.onclick = function(){
          clearInterval(timer);
        };

    停止定时器补充

    注: " " 中必须函数必须全局变量

        function a(){
          console.log(1);
        }
    
        setTimeout(a , 2000);
    
        //前提是,a函数必须是全局变量
        // setTimeout( "a()" , 2000 );
    
    
        //原理上是把 "" 中的代码当成一段JS代码来读
        setTimeout(
          "let x=10;let y=20;console.log(x+y)",
          2000
        );

    定时器传参

        function a( x,y ){
          console.log(x + y);
        }
    
        //如果 参数函数 需要穿入实参
        setTimeout( a , 2000 , 8 , 9 );
  • 相关阅读:
    linux 系统运维工具13款
    Django2.0 分页的应用
    jvm优化
    SSH-key 在Windows下如何生成公钥和私钥
    申请Let’s Encrypt免费证书,给自己网站增加https访问
    wordpress上传主题以及安装插件 出现ftp的问题解决方案
    php压缩文件
    linux下 如何切换到root用户
    TP3.2.3框架隐藏Home模块以及index.php入口文件的方法
    PHP打开错误提示和关闭错误提示的方法
  • 原文地址:https://www.cnblogs.com/yhy-blog/p/14458001.html
Copyright © 2011-2022 走看看