Created
February 8, 2018 09:30
-
-
Save dondevi/b5239d50de78d41c4fac738e7c2de645 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function lazyMan (name) { | |
let man = { name }; | |
let tasks = []; | |
let excute = () => { | |
let task = tasks.shift(); | |
task && task(); | |
}; | |
man.talk = () => { | |
tasks.push(() => { | |
console.log(`Hi! This is ${name}!`); | |
excute(); | |
}); | |
} | |
man.sleep = second => { | |
tasks.push(() => { | |
window.setTimeout(() => { | |
console.log(`Wake up after ${second}`); | |
excute(); | |
}, second * 1000); | |
}); | |
return man; | |
} | |
man.eat = food => { | |
tasks.push(() => { | |
console.log(`Eat ${food}~`); | |
excute(); | |
}); | |
return man; | |
} | |
man.sleepFirst = second => { | |
tasks.unshift(() => { | |
window.setTimeout(() => { | |
console.log(`Wake up after ${second}`); | |
excute(); | |
}, second * 1000); | |
}); | |
return man; | |
}; | |
man.talk(); | |
window.setTimeout(excute); | |
return man; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment