This question already has answers here:
How to randomize (shuffle) a JavaScript array?
(69 answers)
Get a random item from a JavaScript array [duplicate]
(13 answers)
Closed 9 years ago.
I have a repeating page loading function here,
<script>
var interval = 5; // in seconds
var pages = [
'http://example.com/index.php',
'http://example.com/about.php',
'http://example.com/services.php',
'http://example.com/portfolio.php',
'http://example.com/career.php',
'http://example.com/contacts.php'
];
var current_page_index = 0;
setInterval(function() {
loadintoIframe('myframe', pages[current_page_index]);
current_page_index = (current_page_index + 1) % pages.length;
}, interval * 1000); // second setInterval param is milliseconds
</script>
Working fine but I would like to change its loading pattern to RANDOM. Now it is loading as it is given in a pattern.I mean it will first load 'http://example.com/index.php' then 'http://example.com/about.php' like that.
How can I add random effect to it? Someone help me pls....
This question is the extension of Load different pages with in a single iframe
Rather than iterating through your page indices, just get
pages[Math.floor(Math.random()*pages.length)]
If you want to avoid duplication, ie. go through the pages in a random order, then keep your current code but - before the setInterval - shuffle the array. Personally I'd use
pages.sort(function(a,b) {return Math.random()-0.5;}); But I know there are picky people out there who will say this isn't "random enough"... -shrugs-
Related
This question already has answers here:
Generate unique random numbers between 1 and 100
(32 answers)
Closed 2 years ago.
I feel like this should be an easy exercise:
1.- Generate 10 random numbers (0-99) and storage in an array.
2.- Numbers should not repeat.
But all the answer I get from the internet are very complicated or excessive long code.
In the code below I already generate the 10 numbers, but they keep repeating. Any ideas?? (i tried If/else but it didn't work) :(
numbers=[]
for(i=0;i<10;i++){
var oneRandomNum = Math.floor(Math.random()*100);
numbers.push(oneRandomNum);
}
console.log(numbers);
Thank you so much!!!!! :)
You can repeatedly add numbers to a Set, and stop when its size reaches 10:
const set = new Set();
while (set.size !== 10) {
set.add(Math.floor(Math.random() * 100));
}
const numbers = [...set];
console.log(numbers);
This question already has answers here:
Persist javascript variables across pages? [duplicate]
(6 answers)
Persist variables between page loads
(4 answers)
Closed 4 years ago.
I have a speed element in my game however, whenever you fail it refreshes the page.
I do not want people to have to keep resetting their speed every time the page refreshes.
How can I keep the variable the same even after a page refresh?
Thankyou
Here you go, this will do the trick. Just dont set the speed directly though the variable but use the setSpeed function which will update the localstorage aswell.
let speed = localStorage.getItem('speed') ? Number(localStorage.getItem('speed')) : 0; // or whatever your default speed is
function setSpeed(newSpeed) {
speed = newSpeed;
localStorage.setItem('speed', newSpeed);
}
Another solution would be to use get and set methods, this one is more elegant, once you set speed it will automatically be stored in the localstrage.
(function() {
let _speed = localStorage.getItem('speed') ? Number(localStorage.getItem('speed')) : 0;
Object.defineProperty(this, 'speed', {
get: function() {
return _speed;
},
set: function(speed) {
_speed = speed;
localStorage.setItem('speed', speed)
return true;
}
});
})();
This question already has answers here:
Random color generator
(64 answers)
Closed 5 years ago.
I'm working on a little side project where mulitple colours are generated and displayed using javascript. https://mrkwrght.github.io/totallycolours/
my code is currently this :
var randomColor15 = "000000".replace(/0/g,function(){return (~~
(Math.random()*16)).toString(16);});
$(function() {
$("#colorbox").css({
backgroundColor:'#' + randomColor
});
$("#colorcode").text("#" + randomColor);
This block is repeat 16 times.There Must be a easier way of doing this.
I also plan to replace the refresh button with a load more button. but I am unsure how this would be done
You have to put it inside a function and then call
var randomColor15 = function(){return "#"+"000000".replace(/0/g,function(){return (~~(Math.random()*16)).toString(16)})};
//only test
for(var i=0;i<10;i+=1){
console.log(randomColor15());
}
This question already has answers here:
Generate unique number within range (0 - X), keeping a history to prevent duplicates
(4 answers)
Closed 7 years ago.
I know how to sort through an array like this
var rand = myArray[Math.floor(Math.random() * myArray.length)];
but what I am trying to do is use this in a loop to pick values from my array that I haven't picked with this function before.
In other words, let's say my array contains apples, bananas, and oranges. i want to be able to pick all three of those out randomly, but I don't want to pick able to pick out the same one more than once.(I hope this made sense)
You can remove the item from the array, so it will not be selected again
var rand = myArray.length ? myArray.splice(Math.floor(Math.random() * myArray.length), 1)[0] : undefined;
Demo: Fiddle
Note: It will modify the original array, so if you want to keep the original array as it was you need to keep a different copy
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Text blinking jQuery
I'm trying to create a timer in my HTML5/JavaScript game when the game is over. Something like 3...2...1.. where 3 will appear then flash, then 2, then flash, then 1, then flash and return back to my title screen... Nothing I put will get that to work... Anybody able to help me out here? So far my code for the numbers is this:
function CreateTimer(){
timer = setInterval(function() {
cntxt.fillText(time, c.width/2 - cntxt.measureText(time).width/2, c.height/4);
time--;
}, 1000);
}
function resetTimer(){
clearInterval(timer);
time = 3;
where = "menu";
}
But even this will just place the 2nd number and then 3rd number straight over the previous and then return the title as expected.
Thanks in advanced!
You need to clear the text first see http://www.w3schools.com/html5/canvas_clearrect.asp