javascript show progress bar while lengthy javascript calculations are in progress? [closed] - javascript

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I am coding a javascript based network application and in which i have to create array of 1000 rec which generate random numbers between 0 and 1 as
for (var i = 0; i < 1000; i++) {
rec[i] = Math.random();
}
it takes some seconds to generate all random numbers and show it on a div so i just want to ask to how to show progress bar while it generating values?

Are you doing something daft like writing or appending values to the DOM within the loop - thus forcing the browser to try and redraw the screen each iteration...
example 1: fiddle - 4000ms+ (for me)
for (var i = 0; i < 1000; i++) {
rec[i] = Math.random();
document.getElementById('out').innerHTML += ('<br/>'+rec[i]);
}
example 2: fiddle - 10ms (or thereabout)
for (var i = 0; i < 1000; i++) {
rec[i] = Math.random();
}
document.getElementById('out').innerHTML = rec.join('<br/>');
It should not take seconds to generate a 1000 random numbers. In comparison I'm typing this on an 8 year old laptop with a crappy Centrino processor and a simple test produces somewhere in the region of 420,000 random numbers within a second.

Related

My for loop runs only once while trying to reverse an array in javascript [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed last year.
Improve this question
var amount = Number(window.prompt("number of elements"));
var list = []
for (var i = 0; i < amount; i++); {
list.push(window.prompt("enter elements of the list"));
}
list.reverse();
document.write(list);
I tried to reverse a list but whatever amount I enter my code only runs trough for loop once, asks me for element of the list and ends the for loop and prints out my one and only entry since the for loop doesn't ask me for elements multiple times. I'm a beginner it's a silly mistake probably but I just can't figure it out.
As #Barmar pointed out the problem is due to a logic error.
/* Read the item from the prompt until you and write it to the list container. */
function read(list, size){
for(let i = 0 ; i < size ; ++i)
list.push(window.prompt());
}
/* Print a list container */
function print(list){
for(let i = 0 ; i < list.length ; ++i)
console.log(list[i]);
}
var list = []
read(list, Number(window.prompt("number of elements")));
list.reverse();
print(list);

Why is Math.floor(Math.random() * n) better than Date.now() % n to choose a random number between [0, n]? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 3 years ago.
Improve this question
I have been using Math.floor(Math.random() * someArray.length) for years to choose a random element in an array. I have always wondered why it is better than Date.now() % someArray.length. The latter seems to me much less error prone and must be faster (though, I haven't actually benchmarked it).
Notes:
I don't need repeatability.
This isn't run in a loop, so the function is called randomly throughout the application.
This isn't for a simulation, so "more or less" random is good enough.
I have always wondered why it is better than Date.now() % someArray.length
Dates are significantly more complicated than just Math.random - Math.random is a method designed specifically for creating a random number, and is significantly faster:
const p0 = performance.now();
const n = 3;
for (let i = 0; i < 1e6; i++) {
(Date.now() % 3)
}
const p1 = performance.now();
console.log(p1 - p0);
vs
const p0 = performance.now();
const n = 3;
for (let i = 0; i < 1e6; i++) {
Math.floor(Math.random() * 3)
}
const p1 = performance.now();
console.log(p1 - p0);
There's a noticeable bit of overhead when using Dates - plus Dates aren't exactly random. For example, if you're trying to choose a number between 1 and 1000, and the user happens to try to generate an number every 1 second, the resulting numbers may well all be close together, which probably isn't desirable. (Best not to count on user input timing being random)
If semi-decent randomness, security, and performance aren't important goals for your project (which would understandable in a small, casual script), you're free to use Date.now() instead, it won't hurt that much, it's just an unusual thing to do and won't be dependably random. If you want precise, secure randomness, use Crypto.getRandomValues() instead of Math.random().

Javascript plugin for automatic rotation of images [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 7 years ago.
Improve this question
need to let the images disappear and reappear like here (footer at) http://www.guiltypeople.nl/het-bureau/.
Each logo, disappear slowly, and reappear on a different place. Could you help me find a Javascript plugin or something to do this?
thanks
First: To overlap the images you must make a trick with position: absolute or play a bit with background. Check here, here and here.
TIP: If the position it's random and not a loop, you must implement a function which changes location of background hidden object's with jquery or javascript.
Second: When you have the images overlapped, you must make the transition effect. For this you must use a loop and the alpha css property.
In your original question you asked also for rotations and other FX, for this you don't need a plugin, check transforming with css.
Example of changin alpha property in plain javascript:
for (var i = 0; i <= 100;){
var element = document.getElementById('id');
element.style.opacity = i * 0.1;
element.style.filter = 'alpha(opacity=' + i + ')'; // IE fallback
i = i + 10;
}
You can use the iterations to make rotation also, or use less i increment to perform each action when mod condition is true, imagine you want to rotate 90 degrees 4 times and make image disapear in 10 steps:
i = i + 5; // allows 20 iterations
if (i % 25 == 0) // if you want 4 rotation steps
if (i % 10 == 0) // if you want 10 alpha modifications

How do I rearrange an array with similar items so that similar items will never neighbor each other? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
So for example if I have this array:
var input = [1,1,2,4,6,7,7,1];
I want the output to be something like:
[1,2,1,4,6,7,1,7]
The order of the new array does not matter, as long as similar items will never (or at least as seldom as possible) neighbor each other.
I can use plain JavaScript as well as underscore.js.
Try the following:
var input = [1,1,2,4,6,7,7,1];
input.sort()
var output = [];
var len = input.length;
for (var i = 0; i < Math.floor((len / 2)); i++) {
output.push(input[i]);
output.push(input[len - i - 1]);
}
if (len % 2) {
var left_over = input[Math.floor(len / 2)];
if (left_over == output[0]) {
output.push(left_over);
} else {
output.unshift(left_over);
}
}
Or see http://jsfiddle.net/d0j3Lfa3/1.
The solution sorts the numbers then alternates high and low. It deals with an odd number of elements, including corner cases such as [1,1,2] and [1,2,2] where it needs to push the middle element differently to pass. Since the input is sorted, input order doesn't affect the output.
This answer may help simplify things a bit.

How to see if variable is < or equal to 0 [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I need to see if variable is < or equal to 0
Heres my code im working with.
theres other behind the scenes stuff to do with lives but all i need to know
is how to see its value (more below)
function beginCountup() {
var display = document.getElementById("display"),
i = 0;
intervalID = setInterval(function () {
display.textContent = ++i;
}, 26);
lives = lives - 1;
var lcount = document.getElementById('lives')
lcount.innerHTML = lives;
}
So i need to see if lives < or equal to 0 if so i need it to target ElementById('lives')
and make it say game over thanks!
Simple if statement
if(lives<=0){
document.getElementById("lives").innerHTML="Game Over!"
}

Categories

Resources