English 中文(简体)
睡眠没有被定义为同功能?
原标题:sleep is not defined in async function?

Im currently working on a memory game project. A function I am trying to implement, flips cards around after two seconds instead of having them flip instantly.

let openCards = [];

function cardActions(card) {
    // prevent function from adding two classes over and over again 
    if (!(card.classList.contains( open ))) {
        // display the card s symbol 
        card.className +=   open ;
        card.className +=   show ;
        // add card to list of open cards
        openCards.push(card);
        if(openCards.length === 2) {
            if(openCards[0].innerHTML === openCards[1].innerHTML) {
                // add the match class
                Array.from(openCards).forEach(function(card){
                    card.className +=   match ;
                });
                console.log( match );
                // empty open cards
                openCards = [];
            } else {
                Array.from(openCards).forEach(function(card) {
                    // add the mismatch class
                    card.className +=   mismatch ;
                });

在该方案的目前阶段,我计划当使用者已经找回卡片时将卡片lip。

因此,我的确创造了一个叫做“lip”的“yn”功能。 我把等待的睡觉带在休眠方案执行中,但所有休眠的人都没有被确定错误。

我不肯定,为什么这种情况发生,因为睡眠功能IS在短片功能中作了界定。

                // flip cards around
                async function flip() {
                    await sleep(2000);
                    Array.from(openCards).forEach(function(card) {
                        card.classList.remove( mismatch )
                        card.classList.remove( open );
                        card.classList.remove( show );
                    });
                }
                // give user time to look at the cards
                flip();
                console.log( these dont match );
                // empty open cards
                openCards = [];
            }
        }
    }
}
最佳回答
问题回答

处理日期比<条码>> 准时更容易。 如果你想使用<代码>sleep等内容的话,请重新说明,然后确定一种功能,即,在投入后解决:

const sleep = ms => new Promise(res => setTimeout(res, ms));

(async () => {
  console.log( 1 );
  await sleep(500);
  console.log( 2 );
  await sleep(1500);
  console.log( 3 );
})();

它使用<条码>准时和背书。

利用Node.js为我工作:

创造新的功能,在睡觉后解决:

async sleep(ms) {
            return new Promise(resolve => setTimeout(resolve, ms));
          }

使用该功能,仅指作为同功能的职能:

async foobar() {
    await sleep(1000)
}




相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.

热门标签