案例一——猜拳游戏

主要是关于index.js页面中的疑问点:

1、
setInterval({所执行的函数}, 时间间隔)

//开始按钮的函数
  start: function(){
    var that = this;
    //间断
    //timer = setInterval({函数},时间间隔);
    timer = setInterval(function(){
        //每隔50ms执行一次图片切换
      let index = Math.floor(Math.random() * 3);
      that.setData({
        robotImg: '../../images/' + index + '.png'
      })
    }, 50);
  },

或者

start: function(){
    //间断
    //timer = setInterval({函数},时间间隔);
    timer = setInterval(function(){
        //每隔50ms执行一次图片切换
      let index = Math.floor(Math.random() * 3);
      this.setData({
        robotImg: '../../images/' + index + '.png'
      })
    }.bind(this), 50);
  },

1、 ‘../../images/’ + index + ‘.png’ 这是字符串连接
2、.bind(this) :用bind来保持上下值。关于bind参考文章:https://www.cnblogs.com/blfbuaa/p/7110641.html
停止定时器:clearInterval(timer);

2、
Math.random() 获取随机数。大于0小于1
这里写图片描述

Math.floor(Math.random() * 3) 获取0,1,2
这里写图片描述

floor() 方法执行的是向下取整计算,它返回的是小于或等于函数参数,并且与之最接近的整数。
参考:https://www.cnblogs.com/johnsonwei/p/6101171.html

3、
大括号里均为键值对,不能使用等号。
应该为:

属性名:值
tapable:"disable"

4、JS中定义变量的三种方式:const var let
参考文章:https://www.cnblogs.com/mr-wuxiansheng/p/6535062.html

猜你喜欢

转载自blog.csdn.net/gx17864373822/article/details/81069916