Open and close links with intervals [duplicate] - javascript

This question already has answers here:
Open links, one after another
(3 answers)
Closed 8 years ago.
I was trying to make a program that opens a link, closes it after 3 seconds and opens the next one. I'll add more links, but I'd like the program to ask every 10 links "Are you there?". I thought of doing maybe an if statement, but not sure.
<script>
var links = ['www.youtube.com', 'www.yahoo.com', 'www.google.bg', 'www.facebook']
function openLinks(){
window.open(links[0]);
links++
}
function withIntervals(){
setInterval(openLinks(), 3000);
}
</script>

Something like:
var currIndex = 0;
function openLinks(){
window.open(links[currIndex++]);
if (currIndex % 10 == 0) {
if (confirm("Are you there yet?")) {
// do something
}
}
}
When currIndex is divisible by 10, the remainder will be 0, so your if condition will be true.
You'll probably also want to keep the value returned by setInterval so you can clear it later with clearInterval.

Related

How do I send alerts every minute? [duplicate]

This question already has answers here:
JavaScript: get code to run every minute
(3 answers)
Closed 5 years ago.
I'm new to html and javascript but I was wondering how I could go about sending alerts like this https://www.w3schools.com/howto/tryit.asp?filename=tryhow_js_alert every minute or so.
setInterval(() => {
// your code
}, 1000)
https://www.w3schools.com/jsref/met_win_setinterval.asp
javascript have a method to repeat something like a task every so often, this method is the function setInterval this function repeat that you want.
Javascript:
setInterval(function(){
alert("Hello");
}, 3000);
For example this method shown every 3 seconds an alert that say "Hello", if you want repeat a task every 3 minutures you need change 3000 to 180000 like this:
setInterval(function(){
alert("Hello");
}, 180000); // repeat every 3 minutes
Here is the docs of the function.
I hope it helps you, regards!

setInterval 'stacking' up [duplicate]

This question already has answers here:
javascript animation queueing when page does not have focus
(3 answers)
Closed 8 years ago.
Using setInterval to run a function which gives a 'flash' effect to a list.
If I keep the page open, but visit another tab / come back in 10 minutes or so, the setInterval feels like its working every 1 seconds as the function is constantly being called.
Feels to me like its stacking up over time, anyway to fix this?
function flashListItems(){
$('.imageview_navigation li').each(function(i) {
$(this).delay((i++) * 100).fadeTo(200, 0.8).fadeTo(200, 1);
});
}
setInterval(function(){
flashListItems();
}, 10000);
fiddle: http://jsfiddle.net/6w6wrsm0/
There's nothing wrong with your code, some web browers slow these types of intervals down to not cause too much usage. So when the webpage is not used, the fastest a interval can go is usually about 1 sec.
There might be a way to fix this, which is mentioned here:
How can I make setInterval also work when a tab is inactive in Chrome?
Just make your animation function tick by real elapsed time.
var div = $('#my-div');
var leftValue = 0;
var interval = (1000/20); //20fps
var before = new Date();
setInterval(function()
{
now = new Date();
var elapsedTime = (now.getTime() - before.getTime());
if(elapsedTime > interval)
{
//Recover the motion lost while inactive.
leftValue += Math.floor(elapsedTime/interval);
}
else
{
leftValue++;
}
div.css("left", leftValue);
before = new Date();
}, interval);

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