小米前端面试二面"/>
小米前端面试二面
Question:有一个url数组
var urls = ['/a', '/b', '/c',...]
要求依次请求这几个url, 要求是 第一个请求完了才发第二个请求,第二个完了才发第三个请求
如此依次。
var url = [];
var count = 0;
function getData(url){if(count>=url.length) return;count++;Promise.resolve(jQuery.get(url[0])).then(function(data){console.log(data);getData(url.slice(1));});
}
Question:
定义这样一个函数
function repeat(func, times, wait) {
}
参数分别是需要 repeat的函数, repeat的次数,每次repeat的间隔
使用方式如下
调用这个函数能返回一个新函数
,比如传入的是alert
var repeatedFun = repeat(alert, 10, 5000);
调用返回的这个新函数,如: repeatFun("hellworld");
会alert十次 helloworld
setInterval版
function repeat (func, times, wait) {return function(){var _arguments=arguments;var timer = setInterval(function(){func.apply(null,Array.prototype.slice.call(_arguments,0));--times || clearInterval(timer);}, wait);}}
setTimeout版
function repeat (func, times, wait) {return function(){var _arguments=arguments;var that=this;this.timer=function(_t){_t&&clearTimeout(_t);var t=setTimeout(function(){func.apply(null,Array.prototype.slice.call(_arguments,0));--times && that.timer(t);},wait);}this.timer();}}
更多推荐
小米前端面试二面
发布评论