小编Nha*_*Bui的帖子

在 JavaScript 中的另一个(异步)函数完成后执行一个函数

请给我一个普通的 JS 解决方案,因为我是编码新手,引入库只会让我更加困惑。

我在程序中有两个函数:changeText 包含异步 setTimeout 函数,它在 X 秒内淡入淡出文本,而 userNameinput 允许用户输入文本输入,然后在浏览器上显示输入。

我遇到的问题是 usernameinput 与 changeText 函数一起执行。我的目标是让 changeText 函数首先执行并完成,然后让 userNameInput (出现文本输入行)在之后立即执行。

正如您在我的代码中看到的,我已经实现了一个回调以尝试解决这个问题。我创建了一个名为welcome 的新函数,将changeText 和useNameInput 函数捆绑在一起,这样当调用welcome 时,它​​会先执行changeText,完成,然后调用封装在回调中的userNameInput。不知何故,我相信由于 changeText 函数中的 setTimeout 函数在 Javascript 环境之外的队列中放置了 X 时间,JS 看到堆栈中没有任何内容并继续执行 usernameInput 而无需等待。请帮忙!卡了太久了!提前致谢。

HTML:

<div id="h1">Hello,<br></div>
    <div id="inputDiv"></div>
Run Code Online (Sandbox Code Playgroud)

CSS:

 #h1{
      opacity: 0;
      transition: 1s;
}
Run Code Online (Sandbox Code Playgroud)

JS:

function fadeIn() {
  document.getElementById('h1').style.opacity = '1';
}

function fadeOut() {
  document.getElementById('h1').style.opacity = '0';
}

var dialogue = ['Hello,', 'My name is Jane.', 'I have a dog!', 'What is your name?'];

var …
Run Code Online (Sandbox Code Playgroud)

javascript synchronization asynchronous callback

8
推荐指数
1
解决办法
7715
查看次数

分段错误:布尔表达式

我收到错误“分段错误”。我认为它与 isdigit(argv[i]) 行有关,但不明白为什么。

#include <stdio.h>
#include <cs50.h>
#include <math.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>

//implement commandline arguments
int main(int argc, string argv[])
{
    //convert string element in array to integer
    int key = atoi(argv[1]);
    //check that user input for key is no more than 2 memory spots, not a negative number and a single input
    if (argc != 2 || key < 0 || isdigit(argv[1]))
    {
        printf("useage: ./caesar key\n");
        return 1;
    }
    else
    {
    string plaintext = get_string("plaintext: "); …
Run Code Online (Sandbox Code Playgroud)

c cs50

1
推荐指数
1
解决办法
54
查看次数

标签 统计

asynchronous ×1

c ×1

callback ×1

cs50 ×1

javascript ×1

synchronization ×1