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
  • use jQuery instead of $ Commented Feb 12, 2019 at 9:58

2 Answers 2

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>');
        );
    });
  });
});
7
  • still same error. Commented Feb 12, 2019 at 11:56
  • can you try to add $(document).ready(function() and check it again ? pelase check my updated code for more information Commented Feb 12, 2019 at 11:58
  • after using your updated code TypeError: $ is undefined[Learn More] countDown.js:221:5 SyntaxError: expected expression, got '<'[Learn More] Commented Feb 12, 2019 at 12:00
  • did you call countDown.js in your requirejs-config.js file ? Commented Feb 12, 2019 at 12:01
  • yes, check in question, i have added my files in question, Commented Feb 12, 2019 at 12:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.