0

I created a countdown using JS. For that i need jQuery so I created requireJs file.

var config = {
paths: {
    'countDown': "ABC_CoundDown/js/countDown"
},
shim: {
    'countDown': {
        deps: ['jquery']
    }
}
};

then added jquery in web/js/jquery.js

then in phtml I used

<script type="text/javascript">

require([
    'jquery',
    'countDown',
    'domReady'
    ], function(
    $,
) {
    var date = "2019-02-12";
    var myDate = new Date(date);
    myDate.setDate(myDate.getDate() + 2);
    $("#countdown").countdown(myDate, function (event) {
        $(this).html(
            event.strftime(
                '<div class="timer-wrapper"><div class="time">%D</div><span class="text">days</span></div><div class="timer-wrapper"><div class="time">%H</div><span class="text">hrs</span></div><div class="timer-wrapper"><div class="time">%M</div><span class="text">mins</span></div><div class="timer-wrapper"><div class="time">%S</div><span class="text">sec</span></div>'
            )
        );
    });

});

on frontend I'm getting this error instead of my result enter image description here

2

I think you have typo issue in your code so Please replace your code with this and let me know if any issue.

 require(['jquery','countDown','domReady'], function(jQuery) {
   jQuery(document).ready(function(){
    var date = "2019-02-12";
    var myDate = new Date(date);
    myDate.setDate(myDate.getDate() + 2);
    jQuery("#countdown").countdown(myDate, function (event) {
        jQuery(this).html(
            event.strftime('<div class="timer-wrapper"><div class="time">%D</div><span class="text">days</span></div><div class="timer-wrapper"><div class="time">%H</div><span class="text">hrs</span></div><div class="timer-wrapper"><div class="time">%M</div><span class="text">mins</span></div><div class="timer-wrapper"><div class="time">%S</div><span class="text">sec</span></div>');
        );
    });
  });
});
  • still same error. – i_ali Feb 12 at 11:56
  • can you try to add $(document).ready(function() and check it again ? pelase check my updated code for more information – Aasim Goriya Feb 12 at 11:58
  • after using your updated code TypeError: $ is undefined[Learn More] countDown.js:221:5 SyntaxError: expected expression, got '<'[Learn More] – i_ali Feb 12 at 12:00
  • did you call countDown.js in your requirejs-config.js file ? – Aasim Goriya Feb 12 at 12:01
  • yes, check in question, i have added my files in question, – i_ali Feb 12 at 12:02
1

You have a typo in the js code. Remove the , after $ in function($,)

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.