标签: timer

当用户断开连接时如何停止服务器上的计时器?(Node.js/socket.io)

服务器上有一个计时器,当它启动时,就开始倒计时,但是当用户离开页面时,计时器继续运行,并在用户不再在页面上时触发。如何在用户离开页面之前或已经离开时停止此计时器?

    //Timer function
    function startTimer() {
        console.log("Timer statrted!");
        var countdown = 15000;
        setTimeout(function () {
            console.log("Timer done!");
        }, 15000);
    }

    socket.on("get data", function() {
         io.to(room_name).emit("data");
         //call timer
         startTimer(); //"Timer started!", "user disconnected", (after 15s) "Timer done!"
     });



     socket.on("disconnect" ,function(){
         console.log("user disconnected");   
     });
Run Code Online (Sandbox Code Playgroud)

我试图停下clearTimeout()socket.on("disconnect")socket.on("disconnecting") 但它们对已经离开页面的当前用户不起作用...它们只为其他用户触发...

javascript timer node.js socket.io

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

计时器、#selector 解释

我需要一个计时器,所以我使用了这段代码:

timer = Timer.scheduledTimer(timeInterval: 1, target: self, selector:  #selector(generalKnowledge.method), userInfo: nil, repeats: true)
Run Code Online (Sandbox Code Playgroud)

但我不明白#selector。我尝试了多次但没有成功。

timer selector swift

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

Swift - 具有 GCD 和条件的计时器

我使用 Firebase 上传带有进度指示器的文件:

RappleActivityIndicatorView.setProgress(CGFloat(a), textValue: "\(String(a * 100)) %")
print("\(a) %")
Run Code Online (Sandbox Code Playgroud)

我想实现一个条件:如果 % 的值(例如:23%)停留 15 秒或更长时间,则会取消上传。

我正在考虑一个 GCD 计时器:

 DispatchQueue.main.asyncAfter(deadline: .now() + 15) {
        print("We can launch the cancellation of the upload")
    }
Run Code Online (Sandbox Code Playgroud)

但我不知道如何链接 a 值在 15 秒内未更新的条件。任何想法 ?

多谢,

timer grand-central-dispatch swift swift4 dispatch-queue

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

每 x 秒调用 api 不适用于 Ngrx + Angular

例如,我想每 10 秒更新一次列表。我使用了运算符“timer”,但 api 没有在 Chrome -> Network 选项卡上第二次显示。

我的流程:调度加载操作 - 我的 api - 方法 GET - 仅在“网络”选项卡中调用一次。但是,如果我创建了一个带有函数调度该操作的按钮,则每次我单击时,我的 api 都会在“网络”选项卡/Chrome 中被调用多次

我的环境:ngrx 7.4、rxjs 6.5.2、Angular 7 所以我不知道服务器自动阻止重复调用 api 或 Angular 和 ngrx 是否正在使用缓存

// 我的效果

  load$: Observable<Action> = this.action$.pipe(
    ofType<Load>(NotificationActionTypes.load),
    switchMap(() => timer(0, 10000)),
    switchMap((action) => {
      return this.notiService.getNotifications().pipe(map((res: any) => {
        return new LoadSuccess({
          result: res,
        });
      }));
    })
  );```

[![enter image description here][1]][1]
See my image, 20/125 request, 125 continue increasing, 20/130, 20/145, but number 20 didn't increase


  [1]: …
Run Code Online (Sandbox Code Playgroud)

timer polling rxjs ngrx angular

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

Blazor 中的秒表组件

经过大量探索后,我们还没有 blazor 组件秒表。我需要在 blazor 页面中显示经过的时间下面是我的代码,我们可以使用开始和停止按钮计算经过的时间我有点困惑如何在 blazor 页面中调用和显示经过的时间。有什么建议吗?

@using System.Diagnostics;

<p> <button @onclick=@Start>Start</button></p>
<p> <button @onclick=@Stop>Stop</button></p>

@code
{    
    protected override void OnInitialized()
    {
        base.OnInitialized();
        Stopwatch sw = new Stopwatch();
        TimeSpan tt1 = TimeSpan.Zero;    
    }

    private double elapsedtime;
    private DateTime startTime;
    private DateTime stopTime;
    private bool running = false;

    public void Start()
    {
        this.startTime = DateTime.Now;
        this.running = true;
    }

    public void Stop()
    {
        this.stopTime = DateTime.Now;
        this.running = false;
    }

    public double ElapsedTimeSecs()
    {
        TimeSpan interval;
        if (running)
            interval = DateTime.Now …
Run Code Online (Sandbox Code Playgroud)

timer stopwatch blazor

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

在 Windows PowerShell 中使用计时器

我正在尝试弄清楚如何在 Windows PowerShell 中使用计时器。我希望我的脚本运行命令/任务一段时间,停止任务,然后将结果输出到 txt 文件。这是我到目前为止所拥有的:

$timer = [Diagnostics.Stopwatch]::StartNew()

while ($timer.elapsed.totalseconds -lt 10){

netstat 169.254.219.44 

$timer.stop() | 
Out-File $PSScriptroot\connections.txt

break
}
Run Code Online (Sandbox Code Playgroud)

该脚本所做的就是在终端中运行命令,直到我按 ctrl+c。一旦我按 ctrl+c,它就会停止,然后输出 .txt 文件。但 .txt 文件是空白的。我花了比我应该花的时间更多的时间来试图解决这个问题。我是 Windows PowerShell 的新手,只是闲逛看看我能做什么。该脚本没有真正的目的。这简直让我发疯,我无法弄清楚这一点......

windows powershell scripting timer

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

Azure 计时器触发函数“.past_due”返回 false

我正在编写第一个计时器触发的函数,但在将它们配置为自行运行时遇到问题。为了便于解释,我们只关注其中一个。

函数记录为运行成功,但由于记录,我发现每次函数运行时“myTimer.past_due”都会返回“false”,并且函数逻辑不会启动。

我应该如何设置它才能可靠地返回“true”?

我应该如何设置它,以便它可以通过“测试代码”表单门户运行?

我在这里没有得到什么?是否还有其他定时器需要匹配?

我什至通过在 Azure 中设置新功能应用程序和存储帐户来“将其关闭并再次打开”,以确保我在此过程中没有破坏某些设置。

原理:这是一个ETL函数,通过API连接到第三方平台,并将所需数据插入到azure SQL数据库中。我希望它每天凌晨 1 点运行。对于启动函数脚本,我使用了通过 Microsoft DOC 页面提供的脚本,稍加修改,从这里开始: https: //learn.microsoft.com/pl-pl/azure/azure-functions/functions-bindings-timer ?tabs=python

这是我的“ init.py ”:

import datetime
import logging

import azure.functions as func

from Toggl_Data_Procurement_Function import toggl_script


def main(myTimer: func.TimerRequest) -> None:
    utc_timestamp = datetime.datetime.utcnow().replace(
        tzinfo=datetime.timezone.utc).isoformat()

    logging.info(f"myTimer.past_due: {myTimer.past_due}")
    if myTimer.past_due:
        logging.info('Function %s, started by timer trigger, started running at %s', func.Context.function_name, utc_timestamp)
        toggl_script.TogglDataProcurementScript(root_directory=str(func.Context.function_directory)).run()

        utc_timestamp = datetime.datetime.utcnow().replace(
        tzinfo=datetime.timezone.utc).isoformat()

    logging.info('Function %s, started by timer trigger finished running at %s', func.Context.function_name, utc_timestamp)

Run Code Online (Sandbox Code Playgroud)

这是我的 function.json - 为了调试,我将其设置为每 15 …

python timer azure python-3.x azure-functions

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

找时间在一张图中写下一个数字

编写一个函数来计算在javaScript中用一根手指键入数字所需的毫秒数

我试图解决这个问题,但我不知道如何解决这个问题。

javascript time timer milliseconds calculation

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

如何修正 Ruby 倒计时器?

import time

def countdown(time_sec):
  while time_sec:
    mins, secs = divmod(time_sec, 60)
    timeformat = "{:02d}:{:02d}".format(mins, secs)
    print(timeformat, end='\r')
    time.sleep(1)
    time_sec -= 1

  print("Time ended.")
Run Code Online (Sandbox Code Playgroud)

您在上面看到的这段 Python 代码运行顺利,并从给定的 开始倒计时time_sec。此代码还每秒清洁屏幕。我想编写在 Ruby 中工作完全相同的代码。

#timer.rb(Ruby codes) here
def countdown time_sec
    
    while time_sec
        mins, secs = time_sec.divmod(60)[0], time_sec.divmod(60)[1]
        timeformat = "%02d:%02d" % [mins, secs]
        puts "#{timeformat}\r"
        sleep(1)
        time_sec -= 1
    end
    
    puts "Time is ended."
end 
Run Code Online (Sandbox Code Playgroud)

您在上面看到的这段 Ruby 代码以错误的方式工作。首先,秒数被依次打印。但我想像上面的 Python 代码一样更新单行。其次,当运行此 Ruby 代码并达到倒计时时00:00,它会继续从 开始倒计时-01:59。我该如何更正此代码?

ruby time timer carriage-return countdown

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

为什么分配有 time.Afterfunc 的 Timer 变量即使外部 func 退出也会执行其函数

我有一个永远循环,它有一个 Timer 变量,在一段时间后执行一个函数。

package main

import (
    fmt "fmt"
    "time"
)

func exe() {
    fmt.Println("10-seconds-of-time-is-done")
}

func t() {
    var timerCheck *time.Timer
    fmt.Println("here's the timerCheck", timerCheck)
    for {
        timerCheck = time.AfterFunc(10*time.Second, func() { exe() })
        fmt.Println("exiting-from-function-t")
        return
    }
}

func main() {
    t()
    fmt.Println("waiting-inside-main")
    time.Sleep(20 * time.Second)
}

Run Code Online (Sandbox Code Playgroud)

这是输出。我不明白该函数t()立即返回,而是在 10 秒后timerCheck执行其exe()函数。

here's the timerCheck <nil>
exiting-from-function-t
waiting-inside-main
10-seconds-of-time-is-done
Run Code Online (Sandbox Code Playgroud)

这怎么可能?在标准库中,关于AfterFunc

// AfterFunc waits for the duration to elapse and then calls f
// in …
Run Code Online (Sandbox Code Playgroud)

concurrency timer go goroutine

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