Blogger Information
Blog 31
fans 0
comment 0
visits 18238
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
JS第3课-js对象和定时器-九期线上班
Content っ
Original
710 people have browsed it

1.js对象和定时器

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>js对象</title>
  6. </head>
  7. <body>
  8. <button name="send" id="send" onclick="send()">获取验证码</button>
  9. <script>
  10. //定义对象
  11. function func1() {
  12. //第一种创建方式
  13. var obj = new Object();
  14. obj.name = 'jason';
  15. obj.age = 18;
  16. obj.func2 = function () {
  17. console.log(this.name,this.age);
  18. };
  19. obj.func22= function (name,age) {
  20. console.log(name,age);
  21. };
  22. //第二种创建方式
  23. obj.func2();
  24. console.log(obj);
  25. var obj1 = {'name':'jack','age':15};
  26. obj1.func3 = function(){
  27. console.log(obj1.name,obj1.age);
  28. };
  29. obj1.func33 = function(){
  30. obj.func22('li',1);
  31. };
  32. obj1.func3();
  33. console.log(obj1);
  34. var obj2 = {
  35. 'name':'liu',
  36. 'age':12,
  37. objFunc:function () {
  38. console.log(name,age);
  39. },
  40. };
  41. //调用
  42. obj1.func33();
  43. obj2.objFunc();
  44. }
  45. //匿名函数
  46. function func2() {
  47. //setTimeout的使用
  48. setTimeout(function () {
  49. console.log('3秒后运行');
  50. },3000);
  51. function timeFunc() {
  52. console.log('5秒后运行timeFunc()');
  53. }
  54. setTimeout(timeFunc,5000);
  55. }
  56. // setInterval 使用
  57. function func3() {
  58. var f1 = setInterval( function () {
  59. console.log('每三秒调用一次');
  60. //删除定时器
  61. clearInterval(f1);
  62. },3000);
  63. //5次后删除定时器
  64. var count = 0;
  65. var f2 = setInterval(function () {
  66. count++;
  67. console.log('每三秒调用一次'+count);
  68. if (count == 5){
  69. clearInterval(f2);
  70. }
  71. },3000);
  72. }
  73. function send() {
  74. var time = 10;
  75. var f3 = setInterval(function () {
  76. time--;
  77. document.getElementById('send').textContent = time+'秒后重新获取';
  78. if (time == 0){
  79. clearInterval(f3);
  80. document.getElementById('send').textContent = '重新获取';
  81. }
  82. },1000);
  83. }
  84. </script>
  85. </body>
  86. </html>

总结
今天主要学习了对象的创建和使用,函数和匿名函数的使用,还有两种定时器的使用,很好用。

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