Sam
Sam

Reputation: 41

Variable Id not increasing?

In the following code, the variable Id is not increasing with each loop I updated it with my current code

Here is my code, thanks

(function (d, s) {
s = d.createElement('script');
s.src = 'https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.js';
(d.head || d.documentElement).appendChild(s);
s.onload = function () {
    jQuery.noConflict();
    jQuery(function ($) {
        var id = 158066137;;
        var link = 'http://api.roblox.com/marketplace/productinfo?assetId=' + id;
        function scan(val) {
            link = 'http://api.roblox.com/marketplace/productinfo?assetId=' + id;
            $.get(link, function (data) {
                var value = data;
                if (data.Creator.Id == 1 && data.Creator.Name == 'ROBLOX') {
                    var msg = "Created by " + data.Creator.Name + "\nObject Name " + data.Name + "\nAsset Id " + data.AssetId
                    console.log(msg);
                }
            });
        }
        setInterval(function() { scan(true); id++ }, 0);
    });
}
})(document);

Upvotes: 0

Views: 61

Answers (2)

Sukima
Sukima

Reputation: 10084

Your defining the Id inside the function that gets called via setInterval. First don't use setInterval especially if the function is spawning an ajax call. I think you want a setTimeout instead. but none the less let talk about the problem not the implementation choices.

// x => undefined
function foo() {
  var x = 1; // x => 1
  x++; // x => 2
}
foo(); // x => 2
foo(); // x => 2
foo(); // x => 2
// x => undefined

In the example above you never store the variable outside the scope of the function so it goes away and comes back new again.

// x => undefined
function foo() {
  var x = 1; // x => 1
  function bar() {
    x++; // x => x + 1
  }
  bar(); // x => 2
  bar(); // x => 3
  bar(); // x => 4
}
// x => undefined

So you see how calling the same function with a variable at a higher scope will do what you want.

function startPolling() {
  var x = 1;
  function next() {
    console.log(x++);
  }
  setInterval(next, 500);
}

Upvotes: 1

Arun P Johny
Arun P Johny

Reputation: 388406

I think the problem is the line $(document), where $ is referring some other library other than jQuery(which might be using document.querySelectr()).

So as a solution try to move your script to the onload handler of the dynamic script element like

(function (d, s) {
    s = d.createElement('script');
    s.src = 'https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.js';
    (d.head || d.documentElement).appendChild(s);
    s.onload = function () {
        //rever the value of $ to old one
        jQuery.noConflict();
        //use jQuery instead of $ to refer to jQuery object
        jQuery(function ($) {
            setInterval(function () {
                var id = 158066137;
                var link = 'http://api.roblox.com/marketplace/productinfo?assetId=' + id;
                $.get(link, function (data) {
                    var value = data;
                    if (data.Creator.Id == 1 && data.Creator.Name == 'ROBLOX') {
                        var msg = "Created by " + data.Creator.Name + "\nObject Name " + data.Name + "\nAsset Id " + data.AssetId
                        console.log(msg);
                    }
                    id++;
                });
            }, 0);
        });
    }
})(document);

Upvotes: 1

Related Questions