小米前端面试二面

编程入门 行业动态 更新时间:2024-10-09 15:19:16

<a href=https://www.elefans.com/category/jswz/34/1768634.html style=小米前端面试二面"/>

小米前端面试二面

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();}}

更多推荐

小米前端面试二面

本文发布于:2024-02-06 15:41:13,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1749750.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:小米

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!