generate multiple colours with javascript [duplicate] - javascript

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());
}

Related

Why does my code freeze when using for loop but not specified number loop? [duplicate]

This question already has answers here:
How to make for loops in Java increase by increments other than 1
(13 answers)
Closed 8 months ago.
I figured it out but it sure took me 4 hours. It never caused any errors so I used the debug feature which wasn't much help. Since there was no error I'm unsure what else to look up before I ask my question.
for (var i = 0; i < 300; i+7) { //30:00
var random_x = getRandomInt(0, width-1);
var random_y = getRandomInt(0, height-1);
var sample_color = img.colorAt(random_x, random_y);
Solution: change i+7to i++
I'm still confused on why i+7 works when number of loops are specified but not when ran with infinity loop.
The problem with your code is that you aren't assigning the value to i again.
When you write i++ you basically write a shorthand version of i = i + 1. In your code you write i + 7 which doesn't do anything and basically is an infinite loop. You should have written i = i + 7 to assign a new value to i (or i += 7 for the shorthand version).
either use i=i+7 or i+=7
i=i+7 means you are changing the value of i by adding 7 to it. So after every increment value of i increment by 7.
For better knowledge, refer assignment operators.

Javascript issue with localstorage (value changing itself) [duplicate]

This question already has answers here:
What is JavaScript's highest integer value that a number can go to without losing precision?
(21 answers)
Large numbers erroneously rounded in JavaScript
(6 answers)
Closed 5 years ago.
i'm currently trying to store data via localstorage on my website, and if for example I do so :
localStorage.setItem("vue",10206726906969851)
When I want to get the value back I get this result :
localStorage.getItem("vue")
-> "10206726906969852"
So why does the value changes ? Thank you in advance for your help
JavaScript is not epic at precision with numbers. An example:
.2 + .2 = .4
but
.2 + .2 + .2 = 0.6000000000000001
The number you are using is too big for JS to maintain good precision on it. Log the following in your console and you will see what I mean.
10206726906969851 > Number.MAX_SAFE_INTEGER // returns true
The number you are using is too big. I have experienced this in the past. The server will give me numbers that are fine for Java, but too large for JavaScript. So... JS will mess them all up. The only way to fix was to get a shorter number that JS wouldn't barf on.

Autoload Random Pages [duplicate]

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-

Unresponsive Javascript checking lots of elements [duplicate]

This question already has answers here:
Running a long operation in javascript?
(5 answers)
Closed 9 years ago.
I created a brute-force like script which basically needs to check more than 27,000 options, and after each check displays the result inside a div.
The script is coded correctly and if I lower the number of options it works sufficiently well, but if I have many options, after a few seconds, a window pops up telling me that my script is unresponsive. How can I make it responsive while checking this many options.
Oh and I almost forgot, it displays data (which is displayed after every check) only when that pop-up window appears (kinda weird).
Asynchronous batch processing may solve your problem:
var options = ...; // your code
// I assume you are using something like this
function processAll() {
for(var i=0; i<options.length; ++i) ... // causes unresponsivity
}
// try to use this instead
function batchProcessing(from) {
if(from >= options.length) return;
var to = Math.min(1000, options.length-from);
for(var i=from; i<from+to; ++i) ... // your code
// run the next batch asynchronously, let the browser catch the breath
setTimeout(batchProcessing.bind(null, from+1000));
}

Create blinking "text" in javascript [duplicate]

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

Categories