javascript vs php (EM) timestamps

More
5 years 6 days ago #89870 by Nico01
Nico01 created the topic: javascript vs php (EM) timestamps
Hi,

a friend asked me to pimp his survey. One of the requirements was to add a global timer for a group of questions in order to stress the subject.

My first solution was to created an equation that stores a timestamp and then access it from javascript to compute the time difference. But equation functions use php, which runs on the server with a different time from the client.

Then I tried:
<script type="text/javascript" charset="utf-8"> 
jQuery(document).ready(function() {
document.write("{" + parseInt(new Date().getTime()/(1000)) + "}");
});
</script>
But when looking at the question's source code, I realized that brackets are pre-processed.

Then I did:
<script type="text/javascript" charset="utf-8"> 
jQuery(document).ready(function() {
document.getElementById('answer847544X185X1603').value = parseInt(new Date().getTime()/(1000));
});
</script>
This solution has one major problem, I cannot hide the question otherwise the answer isn't set.

At this point I disable the next button, hide the textarea, update the textarea and then skip to the next question.

Is there a better solution to this problem ?

Thank you in advance !

Please Log in to join the conversation.

More
5 years 5 days ago #89877 by Ben_V
Ben_V replied the topic: javascript vs php (EM) timestamps
Hello,
try adding a white-space after the bracket
document.write("{ "

Ben/

Benoît

EM Variables => bit.ly/1TKQyNu | EM Roadmap => bit.ly/1UTrOB4
Last Releases => 2.6x.x goo.gl/ztWfIV | 2.06/2.6.x => bit.ly/1Qv44A1
Demo Surveys => goo.gl/HuR6Xe (already included in /docs/demosurveys)

Please Log in to join the conversation.

More
5 years 5 days ago #89934 by Nico01
Nico01 replied the topic: javascript vs php (EM) timestamps
I tried your proposal, it leads to a weird behavior: it produces a white page with the expected text output and without the usual survey decorations.

But I still cannot access the data.

Please Log in to join the conversation.

More
5 years 5 days ago #89936 by tpartner
tpartner replied the topic: javascript vs php (EM) timestamps
Expression Manager parses everything inside curly braces without preceding and following spaces, so what Ben meant was adding a space after the opening brace and a space before the closing brace:
document.write("{ " + parseInt(new Date().getTime()/(1000)) + " }");

Cheers,
Tony Partner
Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
The following user(s) said Thank You: Ben_V

Please Log in to join the conversation.

More
5 years 5 days ago #89938 by Nico01
Nico01 replied the topic: javascript vs php (EM) timestamps
Well, that's exactly what I tried...
However this isn't a solution.

I want to store a javascript timestamp and access it later. But LimeSurvey's behaviour is to pre-process the curly braces and its content on the server. Then if I print these from javascript, on the client, the server will never know about it.

Please Log in to join the conversation.

More
5 years 5 days ago #89940 by tpartner
tpartner replied the topic: javascript vs php (EM) timestamps
When you say "I cannot hide the question otherwise the answer isn't set.", how are you hiding the question?

You should be able to hide it with JS and still manipulate it. Something like this for a short-text question:
<script type="text/javascript" charset="utf-8"> 
	jQuery(document).ready(function() {
		var qID = 1603
		$('#question'+qID+).hide();
		$('#question'+qID+' input.text').val(parseInt(new Date().getTime()/(1000)));
	});
</script>

Cheers,
Tony Partner
Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.

Please Log in to join the conversation.

More
5 years 5 days ago #89942 by Nico01
Nico01 replied the topic: javascript vs php (EM) timestamps

When you say "I cannot hide the question otherwise the answer isn't set.", how are you hiding the question?


With the option in the advanced parameters, as I do when using equations.

What you proposed is what I said I did at the end of my first post in the topic.

However, the line "$('#question'+qID+).hide();" gave me the idea to try "$('#limesurvey').hide();". But an empty box is still shown.

Please Log in to join the conversation.

More
5 years 5 days ago #89943 by tpartner
tpartner replied the topic: javascript vs php (EM) timestamps

With the option in the advanced parameters, as I do when using equations.

Ah, that param prevents the question from being rendered (which I don't understand) so it can't be manipulated with JavaScript. You'll need to hide it with JS or CSS.

However, the line "$('#question'+qID+).hide();" gave me the idea to try "$('#limesurvey').hide();". But an empty box is still shown.

If you want to hide the whole page while you auto-submit:
$('body').hide()

Cheers,
Tony Partner
Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
The following user(s) said Thank You: Nico01

Please Log in to join the conversation.

Start now!

Just create your account and start using Limesurvey today.

Register now
Join our Newsletter!