1

I have ampscript value which defines how many milliseconds the function should delay the process but can I pass that 'ms' value from ampscript to sleep(?). Below is my script

%%[
var @ms
set @ms="6000"
]%%

<script runat=server>
function sleep(milliseconds) { 
  var start = new Date().getTime(); 
  for (var i = 0; i < 1e7; i++) { 
    if ((new Date().getTime() - start) > milliseconds){
      break; 
    } 
  } 
} 

sleep(6000);

1 Answer 1

3

In your SSJS part, use Variable.GetValue

var milliseconds = Variable.GetValue("@ms")

in the opposite direction, it works with SetValue. See docs for each: GetValue, SetValue

As well as more examples:

https://sfmcgeeks.com/2020/07/22/ampscript-to-ssjs-and-back-again/

2
  • I am not very well versed with SSJS so I am not sure if you are suggesting something like below but I tried it didnt work <script runat="server"> Platform.Load('Core','1'); var milliseconds = Variable.GetValue("@ms") function sleep(milliseconds) { var start = new Date().getTime(); for (var i = 0; i < 1e7; i++) { if ((new Date().getTime() - start) > milliseconds){ break; } } } sleep(milliseconds); </script>
    – Vi Bin
    Commented Aug 6, 2021 at 8:05
  • 1
    Hi, that's probably because you need to make JS understand that milliseconds is an integer. 1) in AMPScript try set @ms= 6000 without double quotes. If that alone doesn't help, then in SSJS force the value to integer like var milliseconds = parseInt(Variable.GetValue("@ms")). Commented Aug 6, 2021 at 9:40

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .