简体   繁体   English

当点击网页上的任何链接或提交按钮时如何在 JavaScript 中检测

[英]How to detect in JavaScript when any link or submit button is clicked on a webpage

I have a web page that shows remote asset data (for example weather station data) and that does background XMLHttpRequest()'s every 5 seconds to our server and reloads the page if new data from the remote asset has been received.我有一个 web 页面,它显示远程资产数据(例如气象站数据),并且每 5 秒向我们的服务器执行一次后台 XMLHttpRequest() 并在收到来自远程资产的新数据时重新加载页面。 This has all been working fine for years.多年来,这一切都运行良好。

The page also has numerous links and submit buttons that can be used to go to other pages or submit commands to the server (which then sends a command to the asset).该页面还有许多链接和提交按钮,可用于 go 到其他页面或向服务器提交命令(然后向资产发送命令)。 Issue I'm having is that some of the commands the server then executes involve calls to 3rd party web services, some of which can occasionally take up to 30 seconds to return or time out.我遇到的问题是服务器随后执行的某些命令涉及对第 3 方 web 服务的调用,其中一些服务有时可能需要长达 30 秒才能返回或超时。 But in the meantime if new data came in from the asset the background JS function reloads the page, thereby interrupting and cancelling the new http request that the user initiated.但与此同时,如果新数据来自资产,后台 JS function 会重新加载页面,从而中断并取消用户发起的新 http 请求。

I could probably work around this by adding onclick or onsubmit tags to every link and submit button to call a function to disable the timer, but as there can be dozens of links on the page I am hoping there might be a simpler, more elegant way where one simple function can tell me if the user clicked on something and thereby initiated a new active http session.我可以通过添加 onclick 或 onsubmit 标签到每个链接并提交按钮来调用 function 来禁用计时器来解决这个问题,但是由于页面上可能有几十个链接,我希望可能有一个更简单、更优雅的方式一个简单的 function 可以告诉我用户是否点击了某些东西,从而启动了一个新的活动 http session。

I enable my script by doing a setTimeout('myCheckServerFunction("'+url+'")',5000);我通过执行setTimeout('myCheckServerFunction("'+url+'")',5000);来启用我的脚本from the html head.来自 html 头。 If the server then tells it there is new data it does a setTimeout(function(){location.reload();},5000);如果服务器告诉它有新数据,它会执行setTimeout(function(){location.reload();},5000);

So I'd like to disable the JS timer and prevent any reload if the user has clicked any link or button and thus if a new http session is active.因此,如果用户单击了任何链接或按钮,因此我想禁用 JS 计时器并防止任何重新加载,因此如果新的 http session 处于活动状态。 Does there exist a function like this?是否存在这样的 function? eg.例如。 something like "window.isNewHttpRequestActive()"?像“window.isNewHttpRequestActive()”这样的东西? Or maybe there's a way I can check if the window.location changed?或者也许有一种方法可以检查 window.location 是否更改? (not sure if that would get updated before the new http request is complete.) (不确定在新的 http 请求完成之前是否会更新。)

Otherwise I could maybe attach a addEventListener() to every link and submit button on the page but I'm a PHP dev not JS so if anyone could recommend the best way to parse the DOM and attach a listener to every link and submit button that would work too.否则,我可能会将 addEventListener() 附加到页面上的每个链接并提交按钮,但我是 PHP 开发人员而不是 JS,所以如果有人可以推荐解析 DOM 的最佳方法并将侦听器附加到每个链接并提交按钮也可以。

I did try looking for events that "bubble" up to higher layers eg.我确实尝试寻找“冒泡”到更高层的事件,例如。 the body element and that will catch link clicks but also catches any click even just a click on any blank area, So not sure how well that would work as I'd still need to filter that event to determine if it actually came from a link or button. body 元素,它会捕捉链接点击,但也会捕捉任何点击,即使只是点击任何空白区域,所以不确定它的效果如何,因为我仍然需要过滤该事件以确定它是否真的来自链接或按钮。 Thank you.谢谢你。

Listening to all click events on body isn't necessarily a bad idea.监听 body 上的所有点击事件不一定是个坏主意。

If you set the handler to use the event variable, you can determine what was clicked specifically:如果将处理程序设置为使用事件变量,则可以确定具体点击了什么:

document.body.addEventListener("click", (event) => {
    if (event.target.tagName === "BUTTON" ||
        event.target.tagName === "A") {
        //Kill the timer
        if (relocationTimeout !== undefined) {
            clearTimeout(relocationTimeout);
        }
    }
});

Of course don't forget to store the timeout reference in a variable beforehand:当然不要忘记事先将超时参考存储在一个变量中:

let relocationTimeout = setTimeout(function(){location.reload();},5000)

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

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