简体   繁体   English

javascript-将按钮单击的处理推迟到页面加载后

[英]javascript - defer processing of a button click until after page loads

I am using Magento and have encountered issues where clicking on the "Add to Cart" button before the page has fully loaded results in inconsistent behaviour. 我使用的是Magento,遇到的问题是在页面完全加载结果之前单击“添加到购物车”按钮会导致行为不一致。

What I would like to do is defer the handler for the button click until after the page loads. 我想做的是将按钮单击的处理程序推迟到页面加载之后。 I was thinking of changing the text of the button to "Adding to cart" so the user knows his click has been registered. 我正在考虑将按钮的文本更改为“添加到购物车”,以便用户知道他的点击已被注册。

What is the best way to defer the handler? 推迟处理程序的最佳方法是什么?

I guess I could register a variable on the click, and then check that variable with a $jQuery.ready() function, but is there a way do that all within the existing handler. 我想我可以在单击时注册一个变量,然后使用$jQuery.ready()函数检查该变量,但是有一种方法可以在现有处理程序中完成所有操作。 Adding an additional code block may not be so easy given how the blocks work in Magento. 考虑到Magento中的代码块如何工作,添加一个额外的代码块可能并不容易。

is there a way do this within the existing [click event] handler 有没有办法在现有的[click event]处理程序中执行此操作

When you make a call to the jquery document ready, it will run when the document is ready - however, if you call this after the document is ready, then the callback will be made immediately. 当调用jquery文档准备就绪时,它将在文档准备就绪时运行-但是,如果在文档准备就绪之后调用它,则将立即进行回调。

So you can change your event handler to: 因此,您可以将事件处理程序更改为:

$(document).on("click", "#mybutton", function() {
    var btn = $(this);
    btn.text("loading...");
    $(function() {
        btn.text("processing...");
        // process button here, example:
        btn.closest("form").submit();
    });
});

If you click the button before the doc is ready, the click event handler's inner doc ready will queue and run when the doc is ready. 如果在文档准备就绪之前单击按钮,则单击事件处理程序的内部文档就绪将在文档准备就绪时排队并运行。

If you click the button after the doc is ready, the click event handler's inner doc ready will run immediately. 如果在文档准备就绪后单击按钮,则click事件处理程序的内部文档就绪将立即运行。

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

相关问题 推迟执行HTML页脚,直到加载Javascript - Defer execution of html footer until Javascript loads 页面加载后/元素加载后单击带有 Javascript 的按钮 - Click button with Javascript after page load / as soon as element loads 单击按钮后,JavaScript代码不等待直到页面加载 - JavaScript code not waiting til page loads after button click 如何完全推迟加载 Google reCaptcha 直到页面完全加载 - How to completely defer loading Google reCaptcha until after the page fully loads 在JavaScript中,有没有一种方法可以推迟对Web套接字消息的处理,直到设置了标志? - In JavaScript is there a way to defer the processing of web socket messages until a flag is set? 在页面完全加载之前禁用点击 - Disable click until page fully loads JavaScript - 单击按钮加载进度条后显示图像 - JavaScript - show image after progress bar loads when click button JavaScript等待,直到加载Facebook按钮 - JavaScript waiting until facebook button loads Chrome Prerender:如何延迟javascript,直到页面实际加载? - Chrome prerender: how to defer a javascript until page is actually loaded? 用于单击页面上每隔几分钟加载一次按钮的 Javascript - Javascript for clicking a button on a page which loads after every few minutes
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM