admin管理员组文章数量:1392086
I have a setInterval
that selects an input and then submits the form with a click action. Now my problem is that my function clicks the button too many times. I tried adding a boolean to check if it has 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 in case the page timeouts or lags.
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.
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);
I have a setInterval
that selects an input and then submits the form with a click action. Now my problem is that my function clicks the button too many times. I tried adding a boolean to check if it has 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 in case the page timeouts or lags.
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.
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);
Share
Improve this question
edited Jul 12, 2021 at 6:44
Jacob van Lingen
9,5977 gold badges54 silver badges83 bronze badges
asked Nov 13, 2015 at 3:18
C OC O
3361 gold badge4 silver badges12 bronze badges
5
-
1
wrap the call in
$(document).ready(function () { /* your code here */ )
– royhowie Commented Nov 13, 2015 at 3:22 -
What is the point of
setInterval
in this case? – StackSlave Commented Nov 13, 2015 at 3:25 - setInterval is for reattempts in a situation where an item bees unavailable, or times out. I use to not have this issue, the setinterval would just stop as soon as the page begins to redirect. My setInterval is now spamming like crazy even when I throw a "window.location='website./checkout.php';" – C O Commented Nov 13, 2015 at 3:27
-
You are want to use
setTimeout
, but it can be unpredictable, you can to addclearInterval(pickSize);
part to your code after click. Better, usewindow.load
event – vp_arth Commented Nov 13, 2015 at 3:55 - you can use this link for Check page is loading in JavaScript – Jalal Malekpour Commented Jul 12, 2021 at 7:58
3 Answers
Reset to default 1You can use onbeforeunload
event to check if the page has started to navigate to another page:
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?" and let's try to understand the reason. It definitely does happen for some reason :)
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. 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.
本文标签: jqueryCheck if page is loading in JavaScriptStack Overflow
版权声明:本文标题:jquery - Check if page is loading in JavaScript? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744781310a2624731.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论