Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
3.8k views
in Technique[技术] by (71.8m points)

javascript - I can't stop Interval in if statement (discord VoiceState)

I can't stop interval declared above the if statement.

const Discord = require('discord.js')
const mysql = require('mysql2')

        const con = mysql.createConnection({
            host: 'localhost',
            user: 'root',
            password: '',
            database: 'nodemysql'
        })

module.exports = client =>{ 

    client.on('voiceStateUpdate', (oldVoiceState, newVoiceState) => {

        const user = newVoiceState

        var intCoin = setInterval(() =>{
            con.query(`UPDATE coins SET coins = coins + 5 WHERE userid = '${user.id}'`)
            console.log('added coins')
        }, 3000)

        function stopcoins(){
            clearInterval(intCoin)
        }

    if(newVoiceState.channel){
        console.log('joined vc')
        if(!newVoiceState.selfMute){
            console.log('notmuted')
            con.query(`SELECT * FROM coins WHERE userid = '${user.id}'`, (err, rows) =>{
                if(err) {
                    console.log(err)
                }
                if(rows.length <1){
                    con.query(`INSERT INTO coins (userid, coins) VALUES ('${user.id}', 0)`)
                    console.log(`added member: ${user.member.user.username}`)  
                } else {
                    intCoin
                }
            })
        }
    } else {
        stopcoins()
        console.log('stop')
    }
    })
}

I tried everything I found. Functions, clearing interval and also changing var to null (in funtion I declared that var is changing into con.query etc.)

EDIT.1 It's my output in console after joining the VC and after some seconds leaving it

joined vc
notmuted
added coins
added coins
added coins
added coins
stop
added coins
added coins

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
等待大神解答

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...