2015-04-17 55 views
-2

我想设置我的Ajax快速购物车,以便如果用户在快速购物车上徘徊(换句话说与它互动),它会延迟我设置的setInterval,以便一旦某件物品已关闭3秒后添加。在Ajax Quick Cart悬停时暂停setInterval,并在没有时恢复?

我不是那么棒,所以任何帮助将不胜感激!

这是我的原代码:

{% comment %} 
Ajaxify your cart with this plugin. 
Documentation: 
    - http://shopify.com/timber#ajax-cart 
{% endcomment %} 
{% if settings.ajax_cart_enable %} 
    {{ 'handlebars.min.js' | asset_url | script_tag }} 
    {% include 'ajax-cart-template' %} 
    {{ 'ajax-cart.js' | asset_url | script_tag }} 
    <script> 
    jQuery(function($) { 
     ajaxCart.init({ 
     formSelector: '#AddToCartForm', 
     cartContainer: '#CartContainer', 
     addToCartSelector: '#AddToCart', 
     cartCountSelector: '#CartCount', 
     cartCostSelector: '#CartCost', 
     moneyFormat: {{ shop.money_format | json }} 
     }); 
    }); 

    jQuery('body').on('ajaxCart.afterCartLoad', function(evt, cart) { 
     // Bind to 'ajaxCart.afterCartLoad' to run any javascript after the cart has loaded in the DOM 

     timber.RightDrawer.open(); 
    }); 

    jQuery('body').on('click','#AddToCart', function(evt, cart) { 

      timber.RightDrawer.open(); 

       var myVar = setInterval(function(){ 
       timber.RightDrawer.close(); 
       clearInterval(myVar); 
       }, 3000); 
    }); 

这是原来的代码,我努力去适应设法得到它的工作,但仍然没有运气,它只是打破我的setInterval的一部分:

jQuery('body').on('click','#AddToCart', function(evt, cart) { 

      timber.RightDrawer.open(); 

       $('#CartContainer').hover(function(ev){ 
        clearInterval(myVar); 
       }, function(ev){ 
        myVar = setInterval(function(){ 
        timber.RightDrawer.close(); 
        clearInterval(myVar); 
        }, 3000); 
       }); 
     }); 
+1

没有答案:但你要使用的mouseenter和鼠标离开。 – epascarello

+0

在这里检查一个很好的解决方案http://stackoverflow.com/questions/10913703/adding-pause-on-hover-to-setinterval – AboutTime

+0

我已经更新了我的问题与我刚刚看过你的例子后尝试,但可以'吨似乎得到它的工作,而不会打破我的setInterval。有什么建议么? – user2498890

回答

0

我不认为这是一种好的方法,正如其他人所评论的那样,有一些潜在的问题需要您按照自己的方式进行操作。

但是,要回答这个问题,下面的JavaScript看起来像描述的那样工作。它看起来像你的主要问题是你没有设置点击按钮后的时间间隔,只有在悬停和取消悬停容器后点击按钮后才设置。

下面,我已将延迟清除移至其自己的功能(doDelayedClear())以防止重复代码,并且当单击该按钮时以及当鼠标悬停并取消悬停时,我都会调用doDelayedClear()

我还添加了一些电话console.log(),以便您可以观看正在发生的事情。

演示http://jsfiddle.net/BenjaminRay/nb9tetLs/

的JavaScript

var myVar = null; 

jQuery('body').on('click', '#AddToCart', function (evt, cart) { 
    timber.RightDrawer.open(); 
    doDelayedClear(); 
    jQuery('#CartContainer').hover(function (ev) { 
     console.log('hovering...'); 
     clearInterval(myVar); 
    }, function (ev) { 
     console.log('not hovering...'); 
     doDelayedClear(); 
    }); 
}); 

function doDelayedClear() { 
    myVar = setInterval(function() { 
     timber.RightDrawer.close(); 
     console.log('Set interval executed!'); 
     clearInterval(myVar); 
    }, 3000); 
} 
+0

嗨,感谢您的回复,我已经尝试了您的建议,但发现尽管这适用于将商品添加到购物车,但它会打开另一个购物车按钮,该按钮仅在点击时打开和关闭。现在该按钮也响应定时器。我很抱歉,因为我对JS不太好,所以觉得有点难以遵循。 – user2498890

+0

请张贴一些HTML,以便我们知道我们正在处理的是什么。应该只有一个ID为AddToCart的按钮,因此如果此代码干扰不同的按钮,则HTML有些问题。 –

+0

我很难展示我的HTML,因为我正在构建Shopify商店及其所有细分市场,但我使用了Shopify Timber框架 - http://timber-demo.myshopify.com - 因此您可以看到我正在工作.. – user2498890