0

This is a very strange issue that is confusing me completely. I will paste the whole code just to make sure nothing is missed.

   echo "
                <script>

                $(function() {


                    $('#biddingload').fadeOut(500);

                    function notice() {
                                    $('#auction_notice').show();
                                    $('#auction_notice').css('background', 'rgba(0, 100, 0, 0.8)');
                                    $('#auction_notice').html('<p>bid accepted!</p>');
                                    setTimeout(function() {
                                            $('#auction_notice').fadeOut();
                                    }, 1000); // <-- time in milliseconds
                        console.log('loaded notice');
                    }
   // notice();

                });

                </script>
                ";

and right below that:

                echo "
                <script>

                $(function() {

notice();
                });

                </script>
                ";

this returns Uncaught ReferenceError: notice is not defined

if I run notice() in the above (commented) part, it works. How this makes sense?

Ok, let me rephrase a question - how is this ($(function() {) a different scope? What would be the right code to call this function afterwards?

Outlaw011
  • 103
  • 10
  • They are two separate scopes. – Amresh Venugopal Jul 28 '16 at 09:06
  • in the first callback set window.notice = function () {...}, as it will be defined in the global scope, you will be able to access this function in the second callback – Olivier Boissé Jul 28 '16 at 09:07
  • I am sorry maybe I do not understand, but isn't function notice() defined in a global scope and later called? – Outlaw011 Jul 28 '16 at 09:12
  • Currently not, when you define a function you create a scope, as you declare the notice function in the scope of the callback function, its only reachable in this scope or in the children scope – Olivier Boissé Jul 28 '16 at 09:14
  • @Outlaw011 no, just keep the function definition outside `$(function(){...})` block. – Amresh Venugopal Jul 28 '16 at 09:15
  • @AmreshVenugopal that's what we did before but sometime it wasn't loaded. So what you are saying is that if we define a function inside $(function(){...}) it is available only in that scope? – Outlaw011 Jul 28 '16 at 09:17
  • @Outlaw011 yes, that construct `$(function(){...})` is a closure. The reason for "wasn't loaded" could be the async nature of the `notice()` function, it has a setTimeout. I can only guess with the available amount of code. I hope it helped. – Amresh Venugopal Jul 28 '16 at 09:21

1 Answers1

0

This is JSHint, a tool that helps to detect errors and potential // problems in your JavaScript code.

Visit http://jshint.com/

Rinto Antony
  • 297
  • 1
  • 10