•

## Answers Categories

< ?php wp_list_categories('show_count=1&title_li=&exclude=341'); ?>

•

•

## Questions Categories

< ?php wp_list_categories('show_count=1&title_li=&child_of=341'); ?>

## JavaScript random function

Q: How do I create a JavaScript random function that does not return two consecutive identical results?

A: Random functions are always interesting topics but this one just caught my eye because the user wanted a random function that did not repeat two numbers in a row. This means 1,2,1,5 is acceptable but 1,1,2,5 is not because there are two returns in a row that have the same value.

Thankfully JavaScript makes this a very easy task for us. We will create a global called randNum and set it equal to 0 and then we will create a function called randNoDups that will take an argument of maxNum (which will be 1 larger than the largest number possible).

The key is to look at our current random number and compare it to our last random number. If they are equal to each other then we need to generate another random number. Here is the code!

<script language=”JavaScript”>
var randNum = 0;
function randNoDups(maxNum)
{
tmpRand = randNum;
while (tmpRand == randNum) {
tmpRand = Math.round(Math.random()*maxNum);
}
randNum = tmpRand;
return tmpRand;
}
</script>

## Calling a function in JavaScript a few seconds after page load

Q: How do you call a function, using JavaScript, in your web browser a few seconds after a page loads? We would like to display a portion of a page for about 5 seconds and then minimize the section after that.

A: To call a function once a page load you want to use the onLoad method in the body tag of your html. To do the delayed call we will need to use the JavaScript function window.setTimeout( function, time ).

The two arguments that the function takes are the function you want to call and the time in milliseconds you want it to wait before calling that function.

Putting both together we would use the code below to achieve what this user was trying to do.

<body onLoad=”window.setTimeout(‘my_function()’, 2000);”>

•