想避免:
function makeTimeout(serial){ serial.close(); } setTimeout(makeTimeout(sp.name),250);
我想做的是某种方式,只需打电话给一个班轮:
setTimeout(function(arg1){ .... }(argument_value),250);
这可以做到吗,还是只能通过无参数函数?
setTimeout(function () { makeTimeout(sp.name); },250);
还有一个选择,使用bind:
bind
setTimeout(makeTimeout.bind(this,sp.name),250);
然而,此功能是ECMAScript第5版功能,在所有主流浏览器中尚不支持.为了兼容,您可以包括MDN中提供的bind的source,允许您在不支持本机的浏览器中使用它.
DEMO.