简体   繁体   中英

javascript hot to make variable global?

i want to change the value of "invocation_num" that inside the loop i did it by this way by it gives my undefiend value any help please

$(document).on("click", '.favoret', function(){
      document.location.href='#favoretList';
      var invocation_num = 0;
      $('#favoret_table').html('');
      itemsDb.transaction(function(transaction) {
        transaction.executeSql('SELECT * FROM favoret GROUP BY title_id', [] , function(transaction, results) {
            $.each(results.rows, function(key, value) {
              var title_count;
              for ( var i =0; i < title_obj.length; i++){
                if(title_obj[i]['id'] == results.rows.item(key).title_id ){

                        itemsDb.transaction(function(transaction,invocation_num) {
                          transaction.executeSql('SELECT * FROM favoret', [], function(transaction, results) {
                            $.each(results.rows, function(key, value) {
                               if(title_obj[i]['id'] == results.rows.item(key).title_id){
                                console.log(results.rows.item(key).title_id);
                                  invocation_num++;
                               }
                            });

                          });
                        });



                    $('#favoret_table').append('<tr class="list-row2 go-invoice" id="'+title_obj[i]['id']+'"><td width="100%" class="middle left-border"><span class="black" style="font-family:Nilland; font-size:17px ; font-weight:200;line-height:1rem">'+title_obj[i]['title']+'  ('+get_count(title_obj[i]['id'])+') </span></td><td  class="middle"><span class="icon arrow orang2">&#61815</span></td></tr>');
                    break;
                }
              }
            });
        });
      });
      // alert(favoret_title);
});

i tried to put it in a function but it doesn't work also

Don't put sql in your javascript

To answer your question, a global variable is defined at the "window" scope. So, to make a variable global, just define it like this

window.somevariable = 'value';

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM