如果函数在3秒内未被调用N个记录中的1个,则如何应用超时 [英] How to apply a timeout if a function hasn't been called 1 of N records in 3 seconds

查看:83
本文介绍了如果函数在3秒内未被调用N个记录中的1个,则如何应用超时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当用户执行某个操作(输入)时,我运行一个功能,在100个用户的列表中将特定样式应用于该用户。随着用户继续键入此功能继续运行。

When a user performs a certain action (typing) I run a function that applies a specific styling to that user in a list of 100s of users. As the user continues to type this function continues to run.

我想要的是应用超时来删除样式。意思是如果在3秒内没有调用该函数,则运行另一个命令来删除样式。

What I would like to do is apply a time out to remove the styling. Meaning if the function hasn't been called in 3 secs, run another command to remove the styling.

有关如何创建这样的超时的任何建议吗?

Any suggestions on how to create a timeout like this?

谢谢

推荐答案

使用JavaScript的 setTimeout()调用方法以在3秒(3000毫秒)后删除样式的方法。见下文:

Use JavaScript's setTimeout() method to invoke a method to remove the styling after 3 seconds (3000 ms). See below:

var timeout;

function clearStyling() {
    // clear your styling here
};

$("#typingBox").on("keypress", function() {
    clearTimeout(timeout);
    timeout = setTimeout(clearStyling, 3000);
    // do your styling here ...
});

这篇关于如果函数在3秒内未被调用N个记录中的1个,则如何应用超时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆