{
let draw=function (count)
{
console.info(`剩余${count}次`);
};
let residue=function*(count)
{
while(count>0)
{
count--;
yield draw(count);
}
};
let star=residue(5);
let btn=document.createElement('button');
btn.id='start';
btn.textContent='抽奖';
document.body.appendChild(btn);
document.getElementById('start').addEventListener('click',
function()
{
star.next();
},false)
}
结果:
点击按钮,结果为:
剩余4次
剩余3次
剩余2次
剩余1次
剩余0次