I am building a interstitial page, using <div> and JavaScript, really simple script but neat.
Everything is working, but I also would like to close the div's after a few seconds (like 10 seconds, for example). Here what I have so far:
I have two div's, 1 and 2.
I have the CSS setup for div 1 like: display: none; (div 1 have the content for the splash screen)
Div 2 is the layer that will cover the page and leave only div 1 visible.
To load the div's I have a onload function like this:
onload="document.getElementById('div1').style.display='block';document.getElementById('div2').style.display='block'"
To hide the divs I have an onclick function like this:
How can I execute the onclick function with a timer, and how can I do it? It also has to be in JavaScript.
I believe you are looking for the setTimeout function.
To make your code a little neater, define a separate function for onclick in a <script> block:
function myClick() {
setTimeout(
function() {
document.getElementById('div1').style.display='none';
document.getElementById('div2').style.display='none';
}, 5000);
}
then call your function from onclick
onclick="myClick();"
setTimeout will help you to execute any JavaScript code based on the time you set.
Syntax
setTimeout(code, millisec, lang)
Usage,
setTimeout("function1()", 1000);
For more details, see http://www.w3schools.com/jsref/met_win_settimeout.asp
onclick = "setTimeout(function() { document.getElementById('div1').style.display='none';document.getElementById('div2').style.display='none'}, 1000)"
Change 1000 to the number of milliseconds you want to delay.
Related
I doing a little project which shows the internet speed by pinging the website and shows the network speed. but the problem is I have to reload every time to get the speed. How to make the div tag which contains the value of the speed to change dynamically.
I tried to load the value of the tag to itself but it doesn't work for me.
The HTML:
<h2> <span id="speed"></span> kbps</h2>
The JS:
kbitsPerSecond has the value of the speed.
$(document).ready( function () {
$('#speed').load('kbitsPerSecond');
refresh();
});
function refresh() {
setTimeout ( function() {
$('#speed').fadeOut('slow').load('kbitsPerSecond').fadeIn('slow);
refresh();
},200);
}
The tag has to be reloaded dynamically
First, you have two syntax problems.
The JQuery .load() method takes a URL as the first argument, you are passing the string 'kbitsPerSecond', which is not a URL:
$('#speed').load('kbitsPerSecond');
Your call to .fadeIn() is missing a closing quote and, if you want the fade in to happen after the .load has completed, you should not chain it after .load, but instead include it in the .load() callback:
$('#speed').fadeOut('slow').load('https://example.com').fadeIn('slow);
Now, setTimeout() is a one-time timer. Instead of making refresh() recursive, use setInterval() which is a continuous timer -- it counts to its supplied interval and then fires its callback function, then it counts again and fires again and so on. However, this will continue even after the page has finished loading, so you'll probably want to cancel the timer at some point.
Also, you don't need two separate .load() calls and a separate function as shown below:
let timer = null; // Will hold a reference to the timer
$(function () {
timer = setInterval (function() {
$('#speed').fadeOut('slow').load('https://example.com', function(){
$('#speed').fadeIn('slow');
});
},200);
});
// Uncomment and add the following to some callback that fires when you no longer want the timer to run
//clearInterval(timer);
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<h2> <span id="speed">TEST</span> kbps</h2>
I'm trying to use JQuery to reload a PartialView in an ASP.Net MVC application.
My code:
<div class="partial">
#Html.Action("GetRemainingSeats", "Layout")
</div>
<script src="#Url.Content("~/Scripts/jquery-1.10.2.min.js")"></script>
<script type="text/javascript">
setTimeout(
$(function () {
setInterval(function () { $('#partial').load('#Html.Action("GetRemainingSeats", "Layout")'); }, 5000)
}), 6000);
</script>
The partial view is loaded correctly, but immediately after that, it will again execute the action, without waiting for the setTimeout function timer to elapse. After that it will stop doing anything.
Edit:
Inside the PartialView there is a table with shows in a cinema together with how many seats are left for each show. So I want that to update very couple of seconds so the employees can see how many seats are left for a show.
I'm using the timeout and setinterval function because on pageload the action is called. After that I want it to wait a couple of seconds before starting the interval
You script is attempting to load html into an element which does not exist (you only have an element with class="partial", not id="partial". You also need to use #Url.Action(), not #Html.Action() (which is a helper that call a child action - it does not generate a url which is expected by the .load() method. You need to make the following changes
html
<div id="partial"> // change this to specify the id attribute
#Html.Action("GetRemainingSeats", "Layout")
</div>
script
$(function () {
setInterval(function () {
$('#partial').load('#Url.Action("GetRemainingSeats", "Layout")'); }, 5000) // use #Url.Action()
}), 6000);
I really don't understand why you use both timeout and setinterval methods as both work same for first time, if you want to refresh div after 4 sec, stick with setInterval method otherwise if you want to refresh only once, use setTimeout method
var wait = setTimeout(
$(function () {
var s = setInterval(function () {
clearInterval(s); // you need to remove the interval if you want to run it once
$('#partial').load('/Layout/GetRemainingSeats');
}, 4000)
}), 6000);
also, you need to change
<script src="#Url.Content("~/Scripts/jquery-1.10.2.min.js")"></script>
to
<script src='#Url.Content("~/Scripts/jquery-1.10.2.min.js")'></script>
I need the javascript code for tooltip to appear onclick for 10 seconds
Mostly Without using html code, or any plugins
$('a.pet_type_link').click(function() {
//I need tooltip to appear for 10seconds
});
I am using coffescript and haml code.
Check this example it is sounds similar to your query
http://jqueryui.com/tooltip/#custom-animation
timeout = setTimeout(function () {
$("#tooltip").hide('fast');//change these options
}, 500);
Are you looking for Tooltip timeout ??
Change the time delay in timeout function as per your requirement.
I am building a interstitial page, using <div> and JavaScript, really simple script but neat.
Everything is working, but I also would like to close the div's after a few seconds (like 10 seconds, for example). Here what I have so far:
I have two div's, 1 and 2.
I have the CSS setup for div 1 like: display: none; (div 1 have the content for the splash screen)
Div 2 is the layer that will cover the page and leave only div 1 visible.
To load the div's I have a onload function like this:
onload="document.getElementById('div1').style.display='block';document.getElementById('div2').style.display='block'"
To hide the divs I have an onclick function like this:
How can I execute the onclick function with a timer, and how can I do it? It also has to be in JavaScript.
I believe you are looking for the setTimeout function.
To make your code a little neater, define a separate function for onclick in a <script> block:
function myClick() {
setTimeout(
function() {
document.getElementById('div1').style.display='none';
document.getElementById('div2').style.display='none';
}, 5000);
}
then call your function from onclick
onclick="myClick();"
setTimeout will help you to execute any JavaScript code based on the time you set.
Syntax
setTimeout(code, millisec, lang)
Usage,
setTimeout("function1()", 1000);
For more details, see http://www.w3schools.com/jsref/met_win_settimeout.asp
onclick = "setTimeout(function() { document.getElementById('div1').style.display='none';document.getElementById('div2').style.display='none'}, 1000)"
Change 1000 to the number of milliseconds you want to delay.
I have banner code, which is not like images and hyperlinks. All I need is the javascript code that will refresh this code every 30 seconds without refreshing the rest of the page.
This banner code will be appear in chatroom. I would really appreciate any help.
The banner code:
<script language='JavaScript'
type='text/javascript'
src='xxxxxx/banner.php?uname=yyyy&type=2&rows=1' >
</script>
First thing you should do is wrap your code in a function. Trying to re-invoke an entire scripting file import is much more difficult than just invoking a function.
Once everything is wrapped in a function, you can use setTimeout to invoke the method. I would do something like this:
function drawBanner() {
// Do stuff to draw the banner.
// Make sure you handle the case that the
// banner is already present in the DOM!
}
function onDrawBannerTimer() {
// Set this function to fire again after 30 seconds.
// Note that this will fire it roughly every 30 seconds real-time.
// You can move this statement to after the drawBanner() call
// in order to make it 30 seconds between the end of one
// invocation and the start of the next.
setTimeout(onDrawBannerTimer, 30 * 1000);
drawBanner();
}
// Trigger the first invocation when the script is loaded.
onDrawBannerTimer();
you'll want to give that script tag an ID (such as id='dynScript') and then have another script block that does pretty much the following: [Note: I'm using jQuery]
<script>
$(document).ready(function(){
setTimeout ( $('#dynScript').attr('src','xxxxxx/banner.php?uname=yyyy&type=2&rows=1'), 3000);
});
</script>