I am working on a bit of code in Javascript that polls a time consuming process that is running in a webservice and returns the status every two seconds. The processPoll function is never getting hit and I can not figure out why the setInterval does not work. I think I have the scope right so I'm not sure why processPoll does not start.
var processId;
var timerId;
function processStartReturn(retVal) {
if ((retVal != null) && (retVal != "")) {
processId = retVal;
timerId = setInterval(processPoll, 2000);
alert(processId); --> alerts correct Id
}
}
function processPoll() {
alert("This alert never shows up!");
WebService.MyFunction(processId, 0);
}
function startPoll() {
var appName = document.getElementById("appName").value;
var threadId = appName + "object";
processStartReturn(threadId);
}
Edit: I have added the startPoll() function that is started with an onclientclick event.
Related
I want to run calculateSomething function for a specific period of time, for example for 1 minute which this function receive messages from MQTT protocol. After 1 minute, this function will sleep or stop receiving data from MQTT for 1 minute, then start to run again.
client.on('message', function (topic, message) {
calculateSomething(topic, message);
})
function calculateSomething(top, param) {
let graph = new Graph();
if(top === 'togenesis') {
graph.addEdgetogenesis(param.toString())
} else if (top === 'DAG'){
graph.addEdge(param.toString())
}
}
I have tried setInterval() but it keep run the function repeatly but I don't want to repeat the function because it is in real time. I also have tried setTimeout() but this only delay for the first time.
Any ideas please how could solve it? thanks in advance.
Try this, the execution of your function is subordinated by a boolean variable that I have named start which serves to keep the function operational (start = true) or not (start = false). The setInterval cycles for one minute and alternates the state of the boolean variable start.
client.on('message', function (topic, message) {
calculateSomething(topic, message);
})
var start = true;
setInterval(function(){
if(start){
start = false;
} else {
start = true;
}
}, 60000); //1 minute
function calculateSomething(top, param) {
if(start){ //the function is executed only if start is true
let graph = new Graph();
if(top === 'togenesis') {
graph.addEdgetogenesis(param.toString())
} else if (top === 'DAG'){
graph.addEdge(param.toString())
}
}
}
I use the recursive function below, in order to reopen website if httpstatus != 200:
retryOpen = function(){
this.thenOpen("http://www.mywebsite.com", function(response){
utils.dump(response.status);
var httpstatus = response.status;
if(httpstatus != 200){
this.echo("FAILED GET WEBSITE, RETRY");
this.then(retryOpen);
} else{
var thisnow = hello[variable];
this.evaluate(function(valueOptionSelect){
$('select#the_id').val(valueOptionSelect);
$('select#the_id').trigger('change');
},thisnow);
}
});
}
The problem is that sometimes the retryOpen function does not even go as far as to callback function(response){}. Then, my script freezes.
I wonder how one could change the function to be able to recursively try to open website again if there is no response from website (not even some error code as 404 or something)? In other words, how to rewrite the retryOpen function so it reruns when the function does not reach callback after a certain amount of time?
I would try something like this. Please note this is untested code, but should get you on the correct path
retryOpen = function(maxretry){
var count = 0;
function makeCall(url)
{
this.thenOpen(url, function(response){
utils.dump(response.status);
});
}
function openIt(){
makeCall.call(this,"http://www.mywebsite.com");
this.waitFor(function check() {
var res = this.status(false);
return res.currentHTTPStatus === 200;
}, function then() {
var thisnow = hello[variable];
this.evaluate(function(valueOptionSelect){
$('select#the_id').val(valueOptionSelect);
$('select#the_id').trigger('change');
},thisnow);
}, function timeout() { // step to execute if check has failed
if(count < maxretry)
{
openIt.call(this);
}
count++
},
1000 //wait 1 sec
);
}
openIt();
}
I was trying to show a text gradually on the screen (like marquee). e.g. H.. He.. Hell.. Hello. when I'm tracing it in debug in VS2010 it's working! but when it's actually running it shows the whole sentence at once.
I made a certain "delay" for about 3 seconds between each letter so it would suppose to take a while, but in reality it's shows everything immediately.
Who's the genius to solve this mystery? (please don't give me advices how to create the marquee effect, it's not the issue anymore. now it's just a WAR between me and javascript!) I'm assuming that it has to do with synchronization when calling function from function?
Thanks to whomever will help me get my sanity back.
you can download the code from here (VS project):
http://pcgroup.co.il/downloads/misc/function_from_function.zip
or view it here:
<body>
<script type="text/javascript">
//trying to display this source sentence letter by letter:
var source = "hi javascript why are you being such a pain";
var target = "";
var pos = 0;
var mayGoOn = false;
//this function calls another function which suppose to "build" the sentence increasing index using the global var pos (it's even working when following it in debug)
function textticker() {
if (pos < source.length) {
flash();
if (mayGoOn == true) {
pos++;
mayGoOn = false;
document.write(target);
textticker();
}
}
}
function flash() {
//I tried to put returns everywhere assuming that this may solve it probably one of them in not necessary but it doesn't solve it
if (mayGoOn == true) { return; }
while (true) {
var d = new Date();
if (d.getSeconds() % 3 == 0) {
//alert('this suppose to happen only in about every 3 seconds');
target = source.substring(0, pos);
mayGoOn = true;
return;
}
}
}
textticker();
</script>
You're obviously doing it wrong. Take a look at this.
var message = "Hello World!";
function print(msg, idx) {
if(!idx) {
idx = 0;
}
$('#hello').html(msg.substring(0, idx));
if(idx < msg.length) {
setTimeout(function() { print(msg, idx + 1) }, 200);
}
}
print(message);
Demo: http://jsbin.com/evehus
I have a requirement on session timeout. For that I am using the session time out function. But actually i need to call this function in all my jsp pages and the time should run properly without any reset when navigating through pages. If the session timeout was reached, it should show the pop up. How can I do this?
The function that I used is given below:
var sFlag = "";
function checkIfContinue()
{
sFlag = 1;
if(confirm("Your Session Expired!. Do you wish to continue?"))
{
window.setTimeout('checkIfContinue()', 15*1000); //start the timer again
sFlag = 0;
}
else
{
window.location = '/XMPortal/jsp/X2ALogin.jsp';
}
}
if( sFlag==0 || sFlag == "")
{
window.setTimeout('checkIfContinue()');
}
How can I call the function in the proper manner?
Im looking at a javascript file trying to figure out a timer issue, but im lost as to what exactly is happening here. Could someone break down this code into bite sizes and explain what is going on?
Timer=0;
function countdown(auctionid) {
var auctions;
var divs;
Timer=Timer+1;
if((Timer%10=="0")||(Timer=="1")) {
$.get("current.php", {
id:auctionid
}, function(data) {
auctions=data.split("||");
for(n=0;n<=auctions.length;n++) {
if(auctions[n] != undefined) {
divis=auctions[n].split("##");
$('#futu'+divis[0]).html(divis[1]);
}
}
});
}
var cauctionid="auctionid";
var tauctions=auctionid.split("|");
for(i=0;i<=tauctions.length;i++) {
if(tauctions[i] != undefined) {
var dd=$('#futu'+tauctions[i]).text();
var cdd=dd-1;
$('#futu'+tauctions[i]).html(cdd);
dd=dd*1000;
dday=Math.floor(dd/(60*60*1000*24)*1)
dhour=Math.floor(dd/(60*60*1000)*1)
dmin=Math.floor((dd%(60*60*1000))/(60*1000)*1)
dsec=Math.floor(((dd%(60*60*1000))%(60*1000))/1000*1)
if(dday==0 && dhour==0 && dmin==0 && dsec==0) {
$('#Bid'+tauctions[i]).html("SOLD");
//return
}
if(dhour <=9) {
dhour = "0"+dhour;
}
if(dmin <=9) {
dmin = "0"+dmin;
}
if(dsec <=9) {
dsec = "0"+dsec;
}
if(dd>=1000) {
var valll=dhour+":"+dmin+":"+dsec;
}
if(dd<1000) {
var valll="00:00:00";
}
$('#Bid'+tauctions[i]).html(valll);
}
}
refreshID = setTimeout("countdown('"+auctionid+"')", 1000);
}
Every second, this script will update the time left for each of the "auctions" on the page. The second argument to setTimeout() is the time to wait in milliseconds, thus 1000 = 1 second.
Also, on the 1st second, and every 10s afterwards, it will make an AJAX call to retrieve a set of auctions in double-pipe (||) delimited string format. It then updates the corresponding auctions on the page with the data from the server.