admin管理员组

文章数量:1425966

I have read several posts about doing "sleep" or "wait" in Javascript. However, they all use client side Javascript. I need to do this in a Scheduled NetSuite SuiteScript. I ahve tried setTimeout() and it tells me it cannot find the function (as well as window.setTimeout()).

If I have to do an infinite loop with an if condition that gives me the delay I want, i will do that but it is less than ideal. I want to know if there is a simple "sleep" or "wait" kind of way of doing this to delay code from executing.

My purpose is because my code deletes records. In my current setup, if 2 of these records are deleted too close to one another NS throws "unexpected error" and stops. if there is a long enough pause in between, then it works. I am trying to automate this so i don't sit here deleting records all day.

The posts I have checked so far: How to create javascript delay function JavaScript.setTimeout JavaScript sleep/wait before continuing What is the JavaScript version of sleep()?

Mine is not a duplicate to any of those as they all assume Client side and are not specific to NetSuite SuiteScript. Thanks!

I have read several posts about doing "sleep" or "wait" in Javascript. However, they all use client side Javascript. I need to do this in a Scheduled NetSuite SuiteScript. I ahve tried setTimeout() and it tells me it cannot find the function (as well as window.setTimeout()).

If I have to do an infinite loop with an if condition that gives me the delay I want, i will do that but it is less than ideal. I want to know if there is a simple "sleep" or "wait" kind of way of doing this to delay code from executing.

My purpose is because my code deletes records. In my current setup, if 2 of these records are deleted too close to one another NS throws "unexpected error" and stops. if there is a long enough pause in between, then it works. I am trying to automate this so i don't sit here deleting records all day.

The posts I have checked so far: How to create javascript delay function JavaScript.setTimeout JavaScript sleep/wait before continuing What is the JavaScript version of sleep()?

Mine is not a duplicate to any of those as they all assume Client side and are not specific to NetSuite SuiteScript. Thanks!

Share Improve this question edited May 23, 2017 at 12:14 CommunityBot 11 silver badge asked Sep 14, 2015 at 17:18 TMannTMann 7812 gold badges15 silver badges37 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 3

Doing a loop based wait might be an overhead, as at times NetSuite might warn of number of script statement.

Another way of doing a sleep can be using nlapiRequestURL() and writing a service on your web server, as it is blocking and synchronous on server side. You can write a HTTP service and in your web server do the sleep job and then respond to the client.

If you are deleting records in a scheduled script then those run serially. Have you tried wrapping the nlapiDeleteRecord call in a try-catch?

If you are getting an error then is a User Event or workflow script running and throwing the error?

As far as a wait I've done the following. It runs the risk of throwing a too many instructions error but avoids a database call that would eat governance. If you can find an nice API call with 0 governance cost that eats some time that would be better but this worked well enough for me.

function pause(waitTime){ //seconds
    try{
        var endTime = new Date().getTime() + waitTime * 1000;
        var now = null;
        do{
            //throw in an API call to eat time
            now = new Date().getTime(); //
        }while(now < endTime);
    }catch (e){
        nlapiLogExecution("ERROR", "not enough sleep");
    }
}

本文标签: javascriptNetSuite Scheduled Script quotSleepquotStack Overflow