码迷,mamicode.com
首页 > 编程语言 > 详细

JavaScript函数setInterval()和setTimeout()正确的写法

时间:2017-08-02 11:57:39      阅读:153      评论:0      收藏:0      [点我收藏+]

标签:字符串   .net   tle   out   执行   lin   pre   href   传参   

一、常规写法

1.1 不传参数

function a (x, y) {
    var i = 0;
    var b = function(){
        console.log((x * y) + (i++));
    }
    return b;
}
var c = a(1, 2);
setInterval(‘c()‘, 1000);

1.2 传参数

function c (x, y) {
    console.log(x * y);
}
setInterval(‘c(1, 2)‘, 1000);

二、改进写法

2.1 不传参数

setInterval(c, 1000);

2.2 传参数

setInterval(function(){
    c(1, 2);
}, 1000);

2.3 改进原因

在幕后,JavaScript仍需要评估和执行你给程序传递的字符串。Link

JavaScript函数setInterval()和setTimeout()正确的写法

JavaScript函数setInterval()和setTimeout()正确的写法

标签:字符串   .net   tle   out   执行   lin   pre   href   传参   

原文地址:http://www.cnblogs.com/mazey/p/7272912.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!