我使用这个jQuery来添加购物车弹出菜单,但无法解决如何添加5秒的超时.

(function($) {
    "use strict";
    
    jQuery(document).mouseup(function (e) {
        
        container = jQuery('.atc-notice-wrapper');
        if (!container.is(e.target) && container.has(e.target).length === 0 ) {
            jQuery('.atc-notice-wrapper').fadeOut();
        }
    }); 

    jQuery(document).ready(function(){

        jQuery('body').append('<div class="atc-notice-wrapper"><div class="atc-notice"></div><div class="close"><i class="fa fa-times-circle"></i></div></div>');

        jQuery('.atc-notice-wrapper .close').on('click', function(){
            jQuery('.atc-notice-wrapper').fadeOut();
            jQuery('.atc-notice').html('');
        });
        var ajaxPId = 0;
        jQuery('body').on( 'adding_to_cart', function(event, button, data) {
            ajaxPId = button.attr('data-product_id');
        });
        jQuery('body').on( 'added_to_cart', function(event, fragments, cart_hash) {
            //get product info by ajax
            jQuery.post(
                ajaxurl, 
                {
                    'action': 'get_productinfo',
                    'data':   {'pid': ajaxPId}
                },
                function(response){
                    jQuery('.atc-notice').html(response);
                    //show product info after added
                    jQuery('.atc-notice-wrapper').fadeIn();
                }
            );
        });
        jQuery('body').on( 'added_to_cart', function(event, fragments, cart_hash) {
            //show product info after added
            jQuery('.atc-notice-wrapper').fadeIn();
        }); 
        
        
function hideQuickView(){
    jQuery('.quickview-wrapper').removeClass('open');
            
    window.setTimeout(function(){
        jQuery('body').removeClass('quickview');
    }, 500);
}


    } );
})( window.jQuery );

我想我需要添加以下内容:

setTimeout(function(){
  $('.atc-notice-wrapper').modal('hide')
}, 5000);

此外,我还想知道是否可以通过子主题添加超时,而无需编辑默认的jQuery.

推荐答案

added_to_cart 是将项目添加到购物车时触发的默认JS事件.

woocommerce Link中的各种活动列表

jQuery('body').on( 'added_to_cart', function(event, fragments, cart_hash) {
 //show product info after added
 jQuery('.atc-notice-wrapper').fadeIn();
 // add setTimeout to autoclose it after 5 seconds
 setTimeout(function(){
   jQuery('.atc-notice-wrapper').fadeOut();
 }, 5000);
});

Jquery相关问答推荐

退出 jquery.each 循环后

如果文本框内容在 X 秒内没有更改,则进行 post 调用

用 jQuery/JavaScript 检测数字或字母?

jQuery 上传进度和 AJAX 文件上传

使用 jQuery 时何时/为什么要在变量前加上$?

jQuery - 向下滚动时缩小的粘性标题

在页面上放置文件时如何设置文件输入值?

清除并刷新 jQuery Chosen 下拉列表

jQuery/JavaScript 碰撞检测

将输入更改为大写

jQuery-UI 的自动完成不能很好地显示,z-index 问题

如何使用 jQuery Select 的插件重置表单?

jquery beforeunload 关闭(不离开)页面时?

如何在 Angular 4 中使用 jQuery 插件?

在不丢失光标位置的情况下更新输入值

Javascript 将 Markdown/Textile 转换为 HTML(理想情况下,返回 Markdown/Textile)

将片段添加到 URL 而不导致重定向?

如何测试 URL 字符串是绝对的还是相对的?

组织 jQuery/JavaScript 代码的最佳方式 (2013)

请求的资源错误中不存在Access-Control-Allow-Origin标头