function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
jaanvivekjaanvivek 

Need help regarding commandButton "Action" and "OnClick"

I am trying to implement online exam functionality in which I would like to restrict user to submit answers till time assigned for that page i.e. 10 minutes.

 

I tried like this in VF page.

<script>
function  myfunction()
    {
     if(c!=0.00)
     {
      var temp;
      temp=setTimeout(function(){alert("You can not submit your answers now ")},1000); 
     
     }
     
     
    
    }
</script>

 This is the syntax for commandButton.

 

<apex:commandButton value="Submit" action="{!submit}" onclick="myfunction();"/>

   But every time when user tries to click on "Submit" button before time end it's allowng to click.

 

I would like to show an alert if user is cliclking on Submit before time end.

 

Could anyone suggest in this case.

 

Thanks,

JaanVivek

 

 

Best Answer chosen by Admin (Salesforce Developers) 
pujapuja

Hi,

Try this....

 

function mySubmit() {
if(c!=0.00){
var temp=setTimeout(function(){alert("You can not submit your answers ")},1000);

return false;
}
}

 

<apex:commandButton value="Submit" action="{!submit}" onclick="return mySubmit();"/>

All Answers

sfdcfoxsfdcfox

myfunction isn't called until the user clicks the button, so they would be allowed to submit the page. Instead, do something like this:

 

<script>
if(window.attachEvent)
  window.attachEvent( 'onload', startTimer )
else
  window.addEventListener( 'load', startTimer, true )

function startTimer( ) {
  setTimeout( 'timeUp()', 60*10*1000) // 10 minutes
}

function timeUp( ) {
  var b = document.getElementById( '{!$Component.theForm.submitButton}' )
  b.disabled = true
  alert( 'The time for this page has expired.' )
}
</script>

You will also probably want to place some sort of timer on the page so they have a visual indication before the button becomes disabled and they are unable to continue.

Balu_SFDCBalu_SFDC

hi ,,

plz do like this it may help you...

 

use the actionpoller componet in vf page and then set intervel time as 10 mints..

if u want error add error message in the submint method;

for example use this code as reference 

 


<apex:page controller="exampleCon">
<apex:form>
<apex:outputText value="Watch this counter: {!count}" id="counter"/>
<apex:actionPoller action="{!incrementCounter}" rerender="counter" interval="15"/>
</apex:form>
</apex:page>


/*** Controller: ***/


public class exampleCon {
Integer count = 0;

public PageReference incrementCounter() {
count++;
return null;
}

public Integer getCount() {
return count;
}
}

.....................................

here u can implement the login in the submit method like

 

if(getcount>=10)//here set ur time in sec's

{

addError();

}

 

regards......

Balu

jaanvivekjaanvivek

Thank you for your suggestions.

 

In my VF Page i used following javascript to show timer in "0.10" format, after every  1 minute it will decrease it by 1 i.e. "0.09" and it will keep on till "0.00".

 

javascript-


<output name="x" id="txt"/>
<script>
var num=0.100;
var c=num.toFixed(2);
var t;
var myVar;
var myVarOne;
function myFunction()
{
t=setInterval(function(){myTimer()},60000);
}

function myTimer()
{
  document.getElementById('txt').value=c;
   var z= 0.010;
   var m=z.toFixed(2);
   c=(c-m).toFixed(2);

   if(c==0.05)
   {
     
     myVar=setTimeout(function(){alert("Only 5 minutes are left")},1000);  

   }
   else if(c==0.00)
    {
       myVarOne=setTimeout(function(){alert("Please submit your answers ")},1000);  
    
    }
   
    else if(c==-0.01)
    {
       clearInterval(t);
    }
 
    }
    
            
    
    </script>
    <body onload="myFunction()"></body>
    

 I it I have applied alert after 5 minutes and an another alert when time left is 1 minute.

 

Now i want to  show one alert if before the end time i.e. 0.00 (after 10 minutes) anyone is trying to click the submit button and page should not be refreshed.

 

 

I have already an VF page and Corresponding controller.

 <apex:commandButton value="Submit" action="{!submit}"/>

 

I was trying to use "onclick" in commandbutton but it did not work.

 

I tried like this.

 

 function mySubmit()
                 {
                 if(c!=0.00)
                {
                var temp=setTimeout(function(){alert("You can not submit your answers ")},1000);  
                }
            }



// VF commandButton

<apex:commandButton value="Submit" action="{!submit}" onclick="mySubmit();"/>

 

 

Please suggest anything in above script.

 

 

Thanks

JaanVivek

pujapuja

Hi,

Try this....

 

function mySubmit() {
if(c!=0.00){
var temp=setTimeout(function(){alert("You can not submit your answers ")},1000);

return false;
}
}

 

<apex:commandButton value="Submit" action="{!submit}" onclick="return mySubmit();"/>

This was selected as the best answer
jaanvivekjaanvivek

Thanks for your reply.

 

I tried it and it was working but othe rthan this my all alert were not working like.

 

 if(c==0.05)
   {
     
     myVar=setTimeout(function(){alert("Only 5 minutes are left")},1000);  

   }

 

 else if(c==0.00)
    {
       myVarOne=setTimeout(function(){alert("Please submit your answers ")},1000);  
    
    }
  

 


here is my whole script.

 

<script>
var num=0.100;
var c=num.toFixed(2);
var t;
var myVar;
var myVarOne;
function myFunction()
{
t=setInterval(function(){myTimer()},8000);
}

function myTimer()
{
  document.getElementById('txt').value=c;
   var z= 0.010;
   var m=z.toFixed(2);
   c=(c-m).toFixed(2);

   if(c==0.05)
   {
     
     myVar=setTimeout(function(){alert("Only 5 minutes are left")},1000);  

   }
   else if(c==0.00)
    {
       myVarOne=setTimeout(function(){alert("Please submit your answers ")},1000);  
    
    }
   
    else if(c==-0.01)
    {
       clearInterval(t);
    }
    
             
        
    }
    
     function mySubmit()
      {
            if(c!=0.00){
            var temp=setTimeout(function(){alert("You can not submit your answers before 5 minutes")},1000);
            
            return false;
            }
    }
   
    </script>
    <body onload="myFunction()"></body> 

 And after time reches to "0.00" it was not submitting the answers.

 

Could you please help in this.

 

 

Thanks,

JaanVivek

jaanvivekjaanvivek

It's working now.

 

there was some network issue.

 

 

Thanks,

JaanVivek