I have this code
<script type="text/javascript">
var currentBlock;
function onSuccessEditUser(result) {
showMessage(result.Message);
window.location = '#Url.Action("UserIndex")';
}
</script>
and I would like to add a little delay after showMessage and before window.location.
How can I do that?
You can use setTimeout to fire off the code after a specified interval:
function onSuccessEditUser(result) {
showMessage(result.Message);
// Wait 1 second
setTimeout(function() {
window.location = '#Url.Action("UserIndex")';
},1000);
}
use java script setTimeOut method to execute something after specified time
<script type="text/javascript">
var currentBlock;
function onSuccessEditUser(result) {
showMessage(result.Message);
// Wait 5 second
setTimeout(function() {
window.location = '#Url.Action("UserIndex")';
},5000);
}
<script>
You could use a setTimeout(function(){showMessage(result.Message);}); function.
Or opt for jQuery $(..).delay(300); http://api.jquery.com/delay/
Whichever you prefer.
Related
I need to trigger a window.open function on the click of body, but only if the click is after few seconds.
EXAMPLE:- if the second click is done immediately, it shouldn't open the window. but after 5 seconds, if the click is made, the window should open.
My code isn't working.
<script>
setInterval(myadFunction,5000);
function myadFunction()
{
$("body").click(function () {
window.open("https://www.google.com");
});
}
</script>
This is a wordpress website., and I entered this code before <body> tag.
Why isn't it working?
You can use a flag to simulate what you want. In this case "canClick" flag will do the job for you.Reset it back to true after your desired timeout.
var canClick = true;
$("body").click(function () {
if (canClick) {
window.open("https://www.google.com");
canClick = false;
setTimeout(() => {
canClick = true
}, 5000);
}
});
Let me know if you face any issue with this snippet.
You could try something like:
<button onclick="timeFunction()">Submit</button>
<script>
function timeFunction() {
setTimeout(function(){ window.open("https://www.google.com"); }, 5000);
}
</script>
It consists of this:
setTimeout(functionname, milliseconds, arg1, arg2, arg3...)
The following are the parameters −
functionname − The function name for the function to be executed.
milliseconds − The number of milliseconds.
arg1, arg2, arg3: These are the arguments passed to the function.
First of all. You should make sure that you are placing the code in the right place. Since it's Wordpress. That bugger really get on my nerves. Try putting it in the active theme.
var click_allowed = 0; //global var (you use const if supported)
setTimeout(function(){ click_allowed = 1; },5000);
jQuery('body').click(function(){
if(click_allowed) window.open("https://www.google.com");
});
jQuery has been used instead of $ for the selectors due to wordpress native jquery limitation.
you can use settimeout(function, millisecond)
I am attempting to delay a function call on button click using javascript in a manner when you click the button it should delay for like 3 or 5 seconds before calling the function. This is my snippet:
jQuery(document).ready(function(){
$('#myButton').on('click', myFunction);
setTimeout(function() {
myFunction();
}, 5000);
function myFunction() {
var text = $(".total").text();
alert(text);
}
});
How can I get it right such that the function is called only when the button is clicked with a delay of 5 seconds?
You can try below code, I done some changes in your code snippet :
jQuery(document).ready(function(){
$('#myButton').on('click', function(){
setTimeout(myFunction, 5000);
});
function myFunction() {
var text = $(".total").text();
alert(text);
}
});
The issue is because you're providing the reference to myFunction() to the click handler, hence it's called immediately.
Instead you need to put the setTimeout() call in an anonymous function that you provide to the click. You also need to place the myFunction() definition in scope of the window so that it's available from the setTimeout() call. Try this:
$(function() {
$('#myButton').on('click', function() {
setTimeout(myFunction, 5000);
});
});
function myFunction() {
var text = $(".total").text();
alert(text);
}
jQuery(document).ready(function(){
$('#myButton').on('click', myFunction);
function myFunction() {
setTimeout(function(){
var text = $(".total").text();
alert(text);
}, 5000);
}
});
script
$(document).ready(function () {
var meter_id = $("#MeterReadingTypes li a.link_active").attr("id");
var range_id = $("#DateRangeTypes li a.link_active").attr("id");
window.setInterval(PostMainChartValues(meter_id, range_id), 5000);
...
});
function PostMainChartValues(meter_id, range_type_id) {
$.ajax({
...
});
}
window.setInterval is not trigerred. If I write an alert in setInterval it works. What is the reason of this? Why function is not triggering? I tracked it with chrome DevTools, and there is no move.
The first parameter to setInterval should be a function (or an evalable string). Right now, you are calling PostMainChartValues() and passing its return value to setInterval().
Change it to:
window.setInterval(function() {
PostMainChartValues(meter_id, range_id);
}, 5000);
This is not an ajax issue. You are using in wrong mode the setInterval parameter.
Create an anonymous function like bellow:
window.setInterval(function () { PostMainChartValues(meter_id, range_id); }, 5000);
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Javascript that executes after page load
script type="text/javascript">
function ip(ban, win) {
var width = 15;
var height = 10;
if (newwin) {
}
</script>
</head>
<b><input type="button" value="Check" onclick="banow();"/></center></b>
Does anyone know if it is possible and how to make this function start on page load, preferably with a interval?
I am new to all this, extra help would be appreciated!
Something like this should work:
window.onload = function() {
setTimeout(banow, 2000);
};
You can just use the onload event, apply the onload event to an event listener, or put a () right after the closing curly bracket of your function.
//e.g. 1:
window.onload = function(){
setTimeout(function(){
function_to_call();
}, 1000);
};
//e.g. 3:
window.addEventListener("load", function(){
setTimeout(function(){
function_to_call();
}, 1000);
}, false);
//e.g. 2:
function function_to_call(){
}();
In jQuery:
$(function(){
// your methods here, will be called after page has been loaded
setInterval(functionName, 1000); //replace functionName with your function, 1000 is millisecond
});
You need body and onload event, please check: http://www.w3schools.com/jsref/event_body_onload.asp
<script type="text/javascript">
window.onload=function(){
alert("hello"); //call your function here
}
</script>
You can call onload in body
eg.
<body onload="ip(parameters)">
</body>
I am trying to use the timeout function within a jsp. But it doesn't work.
<script language="javascript">
function hol_logs() {
var myAjax = new Ajax.Request(
"getlogs.jsp",
{ method: 'get',parameters: 'jobId=<%=job%>', onComplete: zeige_logs }
);
setTimeOut("hol_logs()", 10000);
}
function zeige_logs( originalRequest ) {
$('output').innerHTML = originalRequest.responseText;
}
hol_logs();
</script>
As you can see, the function hol_logs is supposed to be called every 10sec (I also tried it without the (), with no effect). It definitely gets executed once (at the end of the script), but the setTimeOut doesn't seem to work.
Javascript is case sensitive- it should be setTimeout.
You also shouldn't use a string for the code part:
setTimeout(hol_logs, 10000);
It's setTimeout.
Also, wrap your call into function, like this:
setTimeout(function() {
hol_logs();
}, 10000);