0

I've written a script that fires off 2 URLs based on some random number logic and I'm trying to set a delay before either one is fired (of half a second) but I don't think it's working properly. Am I doing this correctly? Code is below:

var clicks  = "http://www.urlone.com";
var impressions = "http://www.urltwo.com";
var randomNumber = (Math.random()*100);

function callOut() {
for (var i = 0; i < lengthVal; i++){
    if (randomNumber < 75) { 
        var randomCounter = (Math.random()*100);
            if (randomCounter < 50) {
                setTimeout("image1.src = clicks;",500);

            }
            else if (randomCounter > 50) {
                setTimeout("image1.src = impressions;",500);                    
            }
    }
    }
}
6
  • 7
    Yes it's wrong. Use function(){ ... } instead of " ... ". And, do not use a loop, but a function + counter (currently, all of your methods fire together after a half second). Commented Feb 22, 2012 at 15:32
  • 1
    what is clicks and impressions? use setTimeout(function(){image1.src=clicks;}, 500); Commented Feb 22, 2012 at 15:32
  • @RobW I've had to "answer my own question" but of course using all this excellent advice. Thanks to all for answers/comments though Commented Feb 22, 2012 at 16:45
  • Your function could never work, because you're not passing a function to setTimeout. Want me to edit your answer, or post a new one? Commented Feb 22, 2012 at 16:47
  • @RobW Please feel free to edit mine :) Commented Feb 22, 2012 at 16:50

4 Answers 4

1

setTimeout first parameter should be a function. Not string of code.

code in the alternate syntax, is a string of code you want to execute after delay milliseconds. (Using this syntax is not recommended for the same reasons as using eval())

MDN

setTimeout(function(){...}, 500);
Sign up to request clarification or add additional context in comments.

3 Comments

A string containing code works too and is valid according to standards. Why are you saying it should be a function name (I take you mean a function reference)?
It is valid and completely legal though. Moreover eval is evil only when used with strings concatenated with variables (e.g. eval("myvar = " + number)).
Yes, and I am questioning that. I know that you did not write "must", but I don't see a reason why it "should" (as I've explained earlier). Let's say we differ in opinions.
1

Taken from here: http://www.codescream.com/?p=18 read it it should help :)

If you want to make a delay with setTimeout you should do exactly this:

setTimeout( function () {
      doThings()
}, 1000);

and never this:

setTimeout( "doThings()", 1000);

Comments

0
setTimeout("image1.src = clicks;",500);

For this image1 must be declared in a global context, like this:

var image1 = document.getElementById('image1');

But you better use a function here.

function setImageSrcClicks(){
    document.getElementById('image1').src = 'http://clicks_url';
}
setTimeout(setImageSrcClicks,500);

2 Comments

Sorry I left this out - it has already been declared, I've just left it out for some bizzare reason :S
If it's not working image1 or clicks is not declared. For debugging try setTimeout("console.log(image1, clicks);",500);
0

Duly noted about using setTimeout with a string. Here is how I ended up doing it. Is this the 'best' way to do this?

var clicks = "http://www.urlone.com";
var impressions = "http://www.urltwo.com";
var conversions = "http://www.urlthree";
var lengthVal = (Math.random() * 20 + 20);
var image1 = new Image();
var image2 = new Image();
var globalCounter = -1;



function callOut() {
    var ord = (Math.random() * 9999999999999) + "";

    var randomNumber = (Math.random() * 100);  // Random value for each call
    if (randomNumber < 75) {
        var randomCounter = Math.random() * 100;
        alert(randomCounter);
        if (randomCounter < 50) {
            image1.src = clicks + ord + "?";
        }
        if (randomCounter > 50) {
            image2.src = impressions + ord + "?";
        }
    }
    if (globalCounter++ < lengthVal) {
        setTimeout(callOut, 1000); // Call itself after another second
    }
}

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.