Should I use Ajax Timer? - ajax

I want to have a stopwatch on the site, which displays running time on the label without reloading a page. Is it possible to do this on client side? Should I use Ajax timer or smth else from .net?
Website is in C#.
Some links or demos would be really helpful ! Thanks !

You can do this with basic JavaScript using setTimeout:
var totalSeconds = 0
function stopwatch() {
// increment the seconds
totalSeconds++;
// display the seconds to the user
document.getElementById("<%=myLabel.ClientID%>").innerHTML = "You have spent " + totalSeconds + " on this page.";
// wait a second and call the timer again
setTimeout("stopwatch()", 1000);
}
// trigger the timer
timer();
Update: If the user is going to be on the page for a while you probably want to display a slightly more user-friendly message then "You have spent 1000 seconds on this page". Here's a quick JavaScript function that'll transform the seconds into time elapsed.

It is possible to do in javascript only. There are so many example out there. Just google them. Here's one.

Related

THREE.Audio filter not ramping up with linearRampToValueAtTime

I'm having a little trouble working with the linearRampToValueAtTime on a BiQuadFilter applied to a WebAudio.
The audio works ok, and the initial lowpass filter is applied.
Problem is, as soon as I use the linearRamp method to bring up the frequency, it seems to ignore the endTime parameter (or better, it's not time correctly).
Some code to explain it better.
Here's the instancing:
this.audioLoader.load( 'public/media/soundtrack-es_cobwebs_in_the_sky.mp3', buffer => {
this.sounds.soundtrack = new THREE.Audio(this.listener);
const audioContext = this.sounds.soundtrack.context;
this.biquadFilter = audioContext.createBiquadFilter();
this.biquadFilter.type = "lowpass"; // Low pass filter
this.biquadFilter.frequency.setValueAtTime(200, audioContext.currentTime);
this.sounds.soundtrack.setBuffer(buffer);
this.sounds.soundtrack.setFilter(this.biquadFilter);
this.sounds.soundtrack.setVolume(0.5);
this.sounds.soundtrack.play();
})
Until here, everything looks ok. The sound plays muffled as needed.
Then, after a certain event, there's a camera transition, where I want the sound to gradually open up.
As a endTime parameter, I'm passing 2 seconds + the internal context delta.
this.sounds.soundtrack.filters[0].frequency.linearRampToValueAtTime(2400, 2 + this.sounds.soundtrack.context.currentTime);
Expecting to hear the ramp in two seconds, but the sound opens up immediately.
What am I missing?
The linear ramp will be applied using the previous event as the startTime. In your case that will be audioContext.currentTime at the point in time when you created the filter. If that is sufficiently long ago it will sound as if the ramp jumps right to the end value. You can fix that by inserting a new event right before the ramp.
const currentTime = this.sounds.soundtrack.context.currentTime;
const filter = this.sounds.soundtrack.filters[0];
filter.frequency.setValueAtTime(200, currentTime);
filter.frequency.linearRampToValueAtTime(2400, currentTime + 2);

How can we use existing selenium scripts (java) to get some end to end performance metrics (low load)

I am trying to leverage existing selenium automation scripts in java developed by Automation team to get some end to end performance metrics (total page load time etc) - very minimal load, in parallel to api load testing.
Please let me know what will be the most efficient way to do that. Also i am looking for making minimum changes to the selenium scripts because this will be an ongoing activity with each release and i am looking for to use functional scripts as it is with performance wrapper around it.
Your suggestions will be appreciated.
thank you!
I would avoid using Selenium for load testing. If setting up dedicated perf tests is not an option, you could at least do this:
//Put this into the loaded scripts of your page under test. Make sure loadTimeMs is a global variable or saved to a hidden control.
var loadTimeMs = 0;
var pageContentRecieved = (new Date()).getTime();
$(window).load(function () {
var pageLoaded = (new Date()).getTime();
loadTimeMs = pageLoaded - pageContentRecieved;
});
That will give you an approximate time to load the sync parts of a page. More importantly is the time for the web server to provide you the content. I recommend doing that in a controlled way by calling the page back as an API in the javascript.
//Return previous value for page load time.
JavascriptExecutor je = (JavascriptExecutor)driver;
int pageLoad = je.executeAsyncScript("return loadTimeMs;");
//Goes into Java code.
je = (JavascriptExecutor)driver;
int apiResponseTime = je.executeAsyncScript(
"var loadTimeMs = 0;" +
"var pageContentRecieved = (new Date()).getTime();" +
"$.ajax({url: "demo_test.txt", success: function(result){" +
"var pageLoaded = (new Date()).getTime();" +
"return pageLoaded - pageContentRecieved;" +
"}});" +
);
int fullLoadTime = pageLoad + apiResponseTime;
Finally, add the two values together for a rough/approximate performance check in the middle of your existing selenium tests.

How to Display hints to user when he is inactive for more than 5 sec in unity?

I have made a 2D Game using unity in which user has to select objects of similar color and now i want to display hint when user is unable to select the color for more than four seconds.
I want something similar to candy crush hint displaying system in which candy crush shows hint by highlighting the possible combination if user is not able to identify any combination himself.
I cannot figure out how to find if the user is inactive so that i can display hints.
I would really appreciate if someone can help me in figuring it out. Thanks in advance.
I dont agree with Joe Blow on this one that you need to call that EVERYWHERE in your code the user does something. What a user can do is press a key on the keyboard(also count on gamepads and controllers) or move the mouse(on mobile the mouse is simulated so that works too). So if you have a single class that looks something like this :
using UnityEngine;
public class TestInActive : MonoBehaviour {
private Vector3 prevMousePosition = Vector3.zero;
void ShowGameHintInvoke()
{
CancelInvoke();
Invoke("GameHint", 5);
}
void GameHint()
{
Debug.Log("This is a Hint");
}
// Update is called once per frame
void Update () {
if (Input.anyKeyDown || Input.mousePosition != prevMousePosition)
ShowGameHintInvoke();
prevMousePosition = Input.mousePosition;
}
}
It should work just fine. This calls ShowGameHintInvoke() once after the user has been inactive for 5 secs. Then it will not call it again until the user does something.
You can use DateTime type and set it on Update, so when the user doesnt do anything, you see all frames if DateTime (now) and the saved DateTime, the difference is more or equal to 5 seconds, there you show.
If the user touches, you flag that the user had done that, so next frame, you will see he didn't touch, and you set again DateTime and start seeing if exceeds 5 seconds.
Do you get what I mean ?
You could use some Coroutines with Yields of 5 seconds (WaitForSeconds) and you put 5 seconds and if it continues the yield, you show hint.

How to add a timing event

Is there a way when I click on a ToolStripButton, it shows the WaitCursor and updates the StatusStrip for about 10 seconds, then returns back to normal. I just don't know how to type in the coding.
If someone could guide me through the process. (or even give me the code)
Thank You
J Mahone
Using a timer:
1:Add a timer from your component toolbox to your form.
2:Set the inteval to 10,000 (this is in milliseconds, 1000 = 1 second)
3:In the timers' "Tick" event, write this code:
Timer1.stop 'This assumes your timer it named Timer1
Me.Cursor = Cursors.Default
4: When you want to make it show the cursor, either have a method to do both these lines and call the method or just write these 2 lines all over the place:
Me.Cursor = Cursors.WaitCursor
Timer1.Start
I'd suggest to use async/await to keep the "normal" program flow.
Me.Cursor = Cursors.WaitCursor
Await Task.Delay(10000)
Me.Cursor = Cursors.Default

How would you use events with Selenium 2

How would one click on a button, wait for an event like blur and then get the pagesource of the site?
I know i can use the getPagesource() method, but I only wanna do this after a jquery loading image has been shown.
If the blur event results in a visible effect, you could wait for that effect, like waiting for an image to be shown.
Otherwise, if there is no visible effect from that event, you would need a "testing hook" to tell your test that the function associated with that event already ran, like a javascript variable being set to a known value that you could query in the test.
For both cases you could use an explicit wait for the condition, like what is shown in the documentation:
http://seleniumhq.org/docs/04_webdriver_advanced.html#explicit-and-implicit-waits
EDIT:
Regarding your comment, Nyegaard, you could use an explicit wait like this one:
WebDriver driver = new FirefoxDriver();
driver.get("http://somedomain/url_that_delays_loading");
Boolean expectedTextAppeared =
(new WebDriverWait(driver, 10))
.until(ExpectedConditions.textToBePresentInElement(
By.id("ctl00_content_createnewschema_modalAlert_alertMessage"), "textYoureExpecting"));
This code will wait for "textYoureExpecting" to appear in the span with a timeout of 10 seconds. If it takes more time for it to appear, you just need to adjust the timeout.
For all AJAX requests in the webpage I use jQuery.Active flag to determine if the page is loaded or not. If jQuery.Active is non-zero that means those are the number of active requests the browser is dealing with. When it comes down to zero, that means number of active requests are none. I haven't used this flag for blur events, but you might as well give it a try. You should definitely use implicitly and explicitly waits Luiz suggested. Here is a function that waits for 5 minutes for active requests to complete. You could perhaps parameterize that, add try, catch etc.
public int waitforAJAXRequestsToComplete(){
long start = System.currentTimeMillis();
long duration;
boolean ajaxNotReady = true;
while(ajaxNotReady){
if(((JavascriptExecutor)driver).executeScript("return jQuery.active").toString().equals("0"))
return 0;
duration = System.currentTimeMillis() - start;
duration = (long) (duration/(60*1000F));
if(duration>5.0)
return 1;
}
return 1;
}

Resources