1

I'd like to share my variable called str with setInterval function. Its needed to build whole url passing to php script. It works fine with ajax function and GET parameter is passing to php script, but I've got some trouble with setInterval function. I dont know to share the same str variable between these two functions. I enclose my code below:

    $(function () {
    $(document).ready(function () {
        var ultimox;
        var ultimoy;
        $('#list li a').on('click',function() {
      var str = $(this).data('driver');

        $.ajax({

                url: "live-data.php" + str,
                type: 'get',
                success: function(DatosRecuperados) {
                $.each(DatosRecuperados, function(i,o){
//some body of function
                });

                setx(DatosRecuperados[(DatosRecuperados.length)-1].x);
                sety(DatosRecuperados[(DatosRecuperados.length)-1].y);

                $('#container').highcharts({
//this part draws chart

        }});

           });  
    });
          setInterval(function () {
                $.get( "live-data.php?Consultar=1" + str , function( UltimosDatos ) {
//this part draws updated chart
                }
           });}, 1000);


          function getx(){return ultimox;}
          function gety(){return ultimoy;}
          function setx(x){ultimox=x;}
          function sety(y){ultimoy=y;}


}); 
4
  • $(function () { is the same as $(document).ready(function (), you do not need to wrap one in the other Commented May 29, 2016 at 14:51
  • Patrick, it doesn't solved my problem. I still have an error that str variable is undefined in this line "$.get( "live-data.php?Consultar=1" + str , function( UltimosDatos ) {" Commented May 29, 2016 at 15:25
  • Didn't say it would, was just letting you know that you didnt need to do it. You are getting the error because you made str a local variable in your click handler. Commented May 29, 2016 at 15:27
  • I've change the scope as you said now it works. Dunno why i've had some problems before. Thank you mate! Commented May 29, 2016 at 16:10

2 Answers 2

2

In JavaScript, scope refers to the current context of your code. Scopes can be globally or locally defined. Understanding JavaScript scope is key to writing good javascript. You’ll need to understand where variables/functions are accessible.

Javascript scope can be thought of as function scoped. You should take the setInterval() function and move it inside $(document).ready(function() { ... });. Since var str; is declared in that function $(document).ready(function() { ... }); the function setInterval can now read str.

I would recommend not polluting the global namespace unless you need to. By this I mean not having the var str; outside of $(document).ready(function() { ... }); . Keep the variable where you need it.

Sign up to request clarification or add additional context in comments.

Comments

1

You can have a global variable in JQuery, try the following example.

<script>
   var str = "";

   $(document).ready(function() {

    //do something with 'str'
    //e.g. str = 'Some value';

   });

   function setInterval()
   {
      //do something with 'str'
      //e.g. str = 'Some other value';
   }
</script>

Comments

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.