小编Mah*_*Ali的帖子

对象中的属性未定义并且clearInterval()不起作用

我有这段代码:

class CombatLog {
constructor(){
    this.idMsg = 0;
    this.timerInterval;
}

startTimer(){
    this.timerInterval = setInterval(this.combatStartLog, 2000);
    $('#combatLog').empty();
}

combatStartLog(){
    console.log(this.idMsg);
    switch (this.idMsg){
        case 3:
            clearInterval(this.timerInterval);
            $('#combatLog').empty();
            break;
        case 2:
            $('<p>', {
                class: 'combatText',
                id: `${this.idMsg}`
            }).appendTo('#combatLog');
            $(`#${this.idMsg}`).append(`FIGHT!`);
            this.idMsg = 3;
            break;
        case 1:
            $('<p>', {
                class: 'combatText',
                id: `${this.idMsg}`
            }).appendTo('#combatLog');
            $(`#${this.idMsg}`).append(`Prepare your potions...`);
            this.idMsg = 2;
            break;
        case 0:
            $('<p>', {
                class: 'combatText',
                id: `${this.idMsg}`
            }).appendTo('#combatLog');
            $(`#${this.idMsg}`).append(`Unsheathe your weapons...`);
            this.idMsg = 1;
        break;
        default:
            this.idMsg = 0;
    }
}
Run Code Online (Sandbox Code Playgroud)

所需的行为将是:

  • 我叫方法 …

javascript constructor setinterval clearinterval

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

对字符串数组进行延迟反应并设置文本值

我有一个加载器,它在传递字符串时显示加载消息,但在传递数组时我想通过消息数组循环显示多个消息。

const messages = ['fetching from sources...', 'loading account...'];

<Loader message={messages}/>

const Loader = (Props) => {
    const { message } = props;


    const renderMessages = (msgs) => {

        console.log(msgs);
        return msgs.forEach((msg, i) => {
            setTimeout(() => {
                return <Message>{msg}</Message>;
            }, 500);
        });
    };

    return (
        <LoaderContainer>
            <LoaderSvg width="120" height="120" viewBox="0 0 100 100" />
            {(Array.isArray(message)) ? renderMessages(message) : <Message>{message}</Message>}

        </LoaderContainer>
    );
};
Run Code Online (Sandbox Code Playgroud)

javascript arrays reactjs

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

为什么 Number('') 和 Number('null') 返回 0

根据MDN

如果参数不能转换为数字,则返回 NaN

有些人可以解释原因Number('')Number(null)返回,0因为没有有效的数字。
首先我认为这可能是因为它们是错误的值。但后来我发现Number(undefined)不返回0

console.log(Number(undefined)); //NaN
console.log(Number(null)); //0
console.log(Number('')); //0
Run Code Online (Sandbox Code Playgroud)

javascript

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

编码它受不需要它编码的 cout 的影响

该程序只是有一个函数来检查一个数字是否是一个完美的数字。完美是其所有因素(不包括数字本身)的总和等于数字本身。6(1 + 2 + 3 = 6)也喜欢28

我看到了一种奇怪的行为。有cout << ""。如果我们删除这一行代码将无法正常工作,如果我们把它一切正常。我无法理解这个

#include<iostream>
using namespace std;

int IsPerfect(int);
int main(){
    
    int N, j;
    cout << "Enter a value for N: ";
    cin >> N;
    cout  << "Perfect numbers are ";
    for(j=1; j<=N; j++){
        cout << ""; //If you remove this line no output will be printed
        if(IsPerfect(j) == 1){
            cout  <<  j << endl;
        }
    }
}

int IsPerfect(int a){
    int sum=0;
    for(int i; i<a; i++){
        if(a%i …
Run Code Online (Sandbox Code Playgroud)

c++ cout

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