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

1

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

1

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. – sudo55 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] – sudo55 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, – sudo55 Feb 12 at 12:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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