简体   繁体   English

检查网页是否正在JavaScript中加载?

[英]Check if page is loading in JavaScript?

so I have a setInterval that selects an input, and then submits the form with a click(). 所以我有一个setInterval,它选择一个输入,然后用click()提交表单。 Now my problem is that my function clicks the button too many times. 现在我的问题是我的函数多次单击按钮。 I tried adding a boolean to check if it's been clicked. 我尝试添加一个布尔值以检查是否已单击它。 No dice, page won't load. 没有骰子,页面将无法加载。 For some odd reason, the submit button doesn't work sometimes. 由于某些奇怪的原因,提交按钮有时不起作用。 I need the interval to be at 100 - 1000 just incase the page timeouts or lags. 我需要将时间间隔设置为100-1000,以防页面超时或滞后。 How do I check to make sure it doesn't click again while the page is trying to load? 我如何检查以确保在尝试加载页面时它不会再次单击? The code can work fine if the interval is above 1000, but I need speed. 如果间隔大于1000,则代码可以正常工作,但是我需要速度。

        var pickSize = setInterval(function(){
            if(window.location.href.indexOf("/website/") == -1 && window.location.href.indexOf("/page.php") == -1) {
                if($('input[value="236"]').attr("checked", true)) {
                    $('.Add').click();
                }
            }
        }, 1000);

Try this: 尝试这个:

var body = document.getElementsByTagName('BODY')[0];
// CONDITION DOES NOT WORK
if ((body && body.readyState == 'loading')) {
DoStuffFunction();
} else {
// CODE BELOW WORKS
if (window.addEventListener) {
    window.addEventListener('load', DoStuffFunction, false);
} else {
    window.attachEvent('onload',DoStuffFunction);
}
}

It's been years and the code is long gone but after revisiting this question I feel that I can offer some insight to those who are having issues with elements not being there. 已经有好几年了,代码早已不复存在,但是在重新讨论了这个问题之后,我觉得我可以为那些遇到问题的人提供一些见解。

The solution to this problem was to wait for the element to finish loading dynamically via an ajax call at the time. 解决此问题的方法是等待该元素当时通过ajax调用动态完成加载。 Once the element is loaded, you now have the ability to click that element. 加载元素后,您现在可以单击该元素。

This is actually something I personally face when creating automated testing and you will see in a lot of selenium code. 实际上,这是我个人在创建自动化测试时所面对的事情,您会在很多硒代码中看到它。 Although, my code above was not selenium and it was just a regular script at the time. 虽然,我上面的代码不是硒,在当时只是一个普通的脚本。 Consider using a library like selenium or puppeteer for any future automated testing. 考虑使用诸如selenium或puppeteer之类的库进行将来的任何自动化测试。

You can use onbeforeunload event to check if the page has started to navigate to another page: 您可以使用onbeforeunload事件来检查该页面是否已开始导航到另一个页面:

var interval = setInterval(function(){
    if(window.location.href.indexOf("/website/") == -1 && window.location.href.indexOf("/page.php") == -1) {
        if($('input[value="236"]').attr("checked", true)) {
            $('.Add').click();
        }
    }
}, 1000);

window.addEventListener('beforeunload', function() {
  clearInterval(interval);
  console.log('Started to navigate away...');
});

However, it looks like you need to fix causes, but not consequences. 但是,您似乎需要解决原因,而不是后果。 Post your HTML and JS into another question like "why does my form submit only sometimes?" 将您的HTML和JS发布到另一个问题,例如“为什么我的表单有时只提交?” and let's try to understand the reason. 让我们尝试了解原因。 It definitely does happen for some reason :) 它确实确实由于某种原因发生了:)

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

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