var obj = {
one1: '苹果',
one2: '梨',
one3: '香蕉',
}
var number = 100
var statistical = {
apple: 0,
pear: 0,
banana: 0,
}
function suiji() {
let num = Math.random()
let a = ''
if (num <= 0.1) {
a = obj.one1
} else if (num <= 0.5) {
a = obj.one2
} else {
a = obj.one3
}
// console.log(a)
return a;
}
function tongji() {
for (let i = 0; i < number; i++) {
(function (i) {
setTimeout(function () {
// console.log(i)
console.log(suiji(), i + 1)
if (suiji() == '苹果') {
statistical.apple++
} else if (suiji() == '梨') {
statistical.pear++
} else {
statistical.banana++
}
if (i + 1 == number) {
console.log(statistical)
return
}
}, (i + 1) * 50);
})(i)
}
}
tongji()
// function sleep(delay) {
// var start = (new Date()).getTime();
// while((new Date()).getTime() - start < delay) {
// continue;
// }
// }
// sleep()
console