简体   繁体   English

更新标签的文本

[英]Updating a label's text

Is there a way to change a labels text every minute or so?有没有办法每分钟左右更改标签文本? If so how?如果有怎么办?

Im trying to make a label with how many online players there is on a specific server.我正在尝试制作一个 label,其中包含特定服务器上有多少在线玩家。 I have made this in javascript and im new to C# so I need some help to get started.我在 javascript 中做了这个,我是 C# 的新手,所以我需要一些帮助才能开始。 I'm grabbing the current players on the server from website which is in JSON.我正在从 JSON 中的网站获取服务器上的当前播放器。 So first I need to parse the players from the other JSON info.所以首先我需要从其他 JSON 信息中解析玩家。 How can I achieve that in C#?如何在 C# 中实现这一点? How can I make it update every minute and recheck if someone has joined/left the server?我怎样才能让它每分钟更新一次并重新检查是否有人加入/离开了服务器? I hope you understand what I mean, else I will gladly explain it better in the comments.我希望你明白我的意思,否则我很乐意在评论中更好地解释它。

The code I made in javascript looks like this:我在 javascript 中编写的代码如下所示:

 http.get("http://IP:PORT/players.json", function(response){
    let json = ""

    // receive everything from the server
    response.on("data", function(chunk){
        json += chunk
    })

    // if received everything
    response.on("end", function(){
        if(response.statusCode == 200){
            try {
                // we parse the response to an "array"
                const players = JSON.parse(json)

                // get the length (so count) and set it as status
                bot.user.setActivity(`${players.length}/64 online`, {
                    type: ''
                })
            }catch(e){
                console.log("Noget gik galt med Status")
            }
        }
    })
})

^ This is made for a discord bot:) ^ 这是为 discord 机器人制作的:)

Sincerely, Ossie真诚的,奥西

You can use crontab or an infinite loop with an AJAX call that should do the job.您可以使用 crontab 或无限循环和 AJAX 调用来完成这项工作。 If you are new to this and performance isn't too big of an issue I would suggest you first look into a loop.如果您对此不熟悉并且性能不是太大的问题,我建议您首先查看循环。 If you need more help let me know如果您需要更多帮助,请告诉我

You can execute a JavaScript function multiple times with a specified time interval您可以以指定的时间间隔多次执行 JavaScript function

window.setInterval(UpdateLabel, 60000); //1 minute = 60000 miliseconds

function UpdateLabel() {
    http.get("http://IP:PORT/players.json", function(response){
        let json = ""

        // receive everything from the server
        response.on("data", function(chunk){
            json += chunk
        })

        // if received everything
        response.on("end", function(){
            if(response.statusCode == 200){
                try {
                    // we parse the response to an "array"
                    const players = JSON.parse(json)

                    // get the length (so count) and set it as status
                    bot.user.setActivity(`${players.length}/64 online`, {
                        type: ''
                    })
                }catch(e){
                    console.log("Noget gik galt med Status")
                }
            }
        })
    });
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM