Blogger Information
Blog 36
fans 2
comment 0
visits 23610
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
12月18号:JS对象,定时器
Rambo-Yang
Original
663 people have browsed it

第一种创建对象方法

  1. //定义对象
  2. var demo = new Object();
  3. //添加对象成员
  4. demo.name = 'rambo';
  5. demo.age = '18';
  6. demo.eat = function(){
  7. console.log('今天吃什么?');
  8. };
  9. demo.eat();
  10. console.log(demo.name);

第二种创建对象的方法

  1. var demo2 = {
  2. "name":"rambo",
  3. "age":[18,19,20],
  4. 'eat':function () {
  5. console.log('今天中午吃什么?');
  6. },
  7. };
  8. // 添加对象元素
  9. demo2.sex = '男';
  10. demo2.eat();
  11. console.log(demo2);

定时器:setTimeout() 只执行一次

  1. setTimeout(function () {
  2. alert('我是延迟5秒弹出来的');
  3. },5000);

第一个参数是匿名函数,如果用命名函数,则只需要填写函数名,不需要加()

定时器:setInterval() 循环执行

  1. res = setInterval(function () {
  2. console.log('弹出文字');
  3. clearInterval(res); //取消执行
  4. },2000)

不结束执行就会一直弹,隔2秒弹一次,结束执行函数是clearInterval()

案例1,手机短信发送验证码倒计时重发功能

  1. <button type="button" id="yzm" onclick="yzm()">发送验证码</button>
  2. <script>
  3. function yzm() {
  4. flag = 10;
  5. timer = setInterval(frame,1000);
  6. var text = document.getElementById('yzm').textContent;
  7. function frame() {
  8. // document.getElementById('yzm').textContent = flag + '秒后重发';
  9. document.getElementById('yzm').innerText = flag + '秒后重发';
  10. flag--;
  11. if (flag <10){
  12. document.getElementById('yzm').disabled = 'disabled';
  13. }
  14. if (flag < 0){
  15. // document.getElementById('yzm').textContent = text;
  16. document.getElementById('yzm').innerText = text;
  17. document.getElementById('yzm').disabled = '';
  18. clearInterval(timer);
  19. }
  20. }
  21. }
  22. </script>

textContentinnerText 都是获取第一个子元素的内容。

案例2 进度条

  1. <style>
  2. #myProgress {
  3. width: 100%;
  4. height: 30px;
  5. position: relative;
  6. background-color: #ddd;
  7. }
  8. #myBar {
  9. background-color: #4CAF50;
  10. width: 10px;
  11. height: 30px;
  12. position: absolute;
  13. }
  14. </style>
  15. <div id="myProgress">
  16. <div id="myBar"></div>
  17. </div>
  18. <br>
  19. <button id="btn" onclick="move()">点我</button>
  20. <script>
  21. function move() {
  22. var flag= 0;
  23. var myBar = document.getElementById('myBar');
  24. var w = setInterval(frame,10);
  25. function frame() {
  26. flag ++;
  27. myBar.style.width = flag + '%';
  28. if (flag > 1 ){
  29. document.getElementById('btn').disabled = 'disabled';
  30. }
  31. if (flag == 100){
  32. clearInterval(w);
  33. document.getElementById('btn').disabled = '';
  34. }
  35. }
  36. }
  37. </script>

Correction status:Uncorrected

Teacher's comments:
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post